勉強したことのメモ

Webエンジニア / プログラマが勉強したことのメモ。

スクロールしてもついてくる追尾型の広告を作る方法

   2024/01/13  jQuery

スマホサイトとかでよく見る、スクロールしてもページ下部とかに常時表示されている追尾型の広告を作りたかった。というのも大体の場合JSタグをそのまま発行してくれるんだけど、今回はバナー用画像とURLのみ発行します、というパターンだった。

調べてみるとjQueryのMeerkatというプラグインが楽に実装できそう。以下にサンプルとソースのメモ。

 

Meerkat

http://meerkat.jarodtaylor.com/

 

サンプル

https://taitan916.info/sample/meerkat/

 

ソース

<!DOCTYPE html>
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jquery.meerkatのサンプル</title>
<style>
.pos-bot .ad_area {
    left: 0;
    margin: 0 auto;
    position: absolute;
    right: 0;
    width: 320px;
}
.m200{
    margin-bottom:200px;
}
</style>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery-migrate-1.0.0.js"></script><!-- ←jqueryのバージョンが1.9未満なら多分不要 -->
<script type="text/javascript" src="jquery.meerkat.1.3.js"></script>
<script type="text/javascript">
$(function(){
    $('.meerkat').meerkat({
        height: '100px',
        width: '100%',
        position: 'bottom',
        animationIn: 'slide',
        animationSpeed: 500,
        removeCookie: '.reset'
    }).addClass('pos-bot');
});
</script>
</head>
<body>
    <div>
        <p class="m200">text</p>
        <p class="m200">text</p>
        <p class="m200">text</p>
        <p class="m200">text</p>
        <p class="m200">text</p>
    </div>
    <div class="meerkat">
        <div class="ad_area">
            <a href="http://google.com" target="_blank" class="ad_link">
                <img src="banner.png" class="ad_img">
            </a>
        </div>
    </div>
</body>
</html>

 

その他

jQuery1.9以降を使う場合はjquery-migrateを使わないとエラーが出るので注意。

https://github.com/jquery/jquery-migrate

 - jQuery

  関連記事

jQueryからAjax経由でPHPにリクエストしCookieを操作する方法

先日PHPでCookie保存時にHttpOnly / Secure属性を設定する ...

フォームの入力内容をjQueryで取得し、本文として設定した上でメーラーを起動させる方法

formで何らかを入力してもらいボタンをクリックするとメーラーが開き、先ほど入力 ...

jQueryで特定のdata属性をセレクタとして指定する方法

jQueryで特定のdata属性をクリックした際に何らかのイベントを実行したかっ ...

jQueryで画像(タグ内)のsrc情報を取得する場合

すぐに出てこなかったのでメモ。 $('#test').attr('src'); ...

エリア→都道府県→路線→駅名を連携する絞り込みセレクトメニューの設置方法

エリア→都道府県→路線→駅名のドリルダウン検索をプルダウン形式で設置したい。以前 ...