2010年7月13日 星期二

跟著捲軸移動的廣告

HTML ->


<div class="bigDiv">我是一個很長的區塊</div>

<div id="abgne_float_ad" >
<span class="abgne_close_ad">關閉廣告 [X]</span>
<a href="http://www.flycan.com.tw/course/course_javascript.php">
<img src="course_javascript.gif" title="JavaScript & CSS 互動程式入門班" />
</a>
</div>
</body>




CSS ->


display: none;
position: absolute;
}
#abgne_float_ad .abgne_close_ad {
display: block;
text-align: right;
cursor: pointer;
font-size: 12px;
}
#abgne_float_ad a img {
border: none;
}
div.bigDiv {
height: 3000px;
}



Javascript ->


$(window).load(function(){
var $win = $(window),
$ad = $('#abgne_float_ad').css('opacity', 0).show(), // 讓廣告區塊變透明且顯示出來
_width = $ad.width(),
_height = $ad.height(),
_diffY = 20, _diffX = 20, // 距離右及下方邊距
_moveSpeed = 800; // 移動的速度

// 先把 #abgne_float_ad 移動到定點
$ad.css({
top: $(document).height(),
left: $win.width() - _width - _diffX,
opacity: 1
});

// 幫網頁加上 scroll 及 resize 事件
$win.bind('scroll resize', function(){
var $this = $(this);

// 控制 #abgne_float_ad 的移動
$ad.stop().animate({
top: $this.scrollTop() + $this.height() - _height - _diffY,
left: $this.scrollLeft() + $this.width() - _width - _diffX
}, _moveSpeed);
}).scroll(); // 觸發一次 scroll()

// 關閉廣告
$('#abgne_float_ad .abgne_close_ad').click(function(){
$ad.hide();
});
});



範例瀏覽:
http://demonstration.abgne.tw/jquery/0024/0024_1.html
http://demonstration.abgne.tw/jquery/0024/0024_2.html
http://demonstration.abgne.tw/jquery/0024/0024_3.html(可關閉廣告)
http://demonstration.abgne.tw/jquery/0024/0024_4.html(位置在上)
http://demonstration.abgne.tw/jquery/0024/0024_5.html(位置在左上)

Ext JS 版:
http://demonstration.abgne.tw/extjs_3_x/0024/0024_1.html
http://demonstration.abgne.tw/extjs_3_x/0024/0024_2.html(可關閉廣告)
http://demonstration.abgne.tw/extjs_3_x/0024/0024_3.html(位置在上)
http://demonstration.abgne.tw/extjs_3_x/0024/0024_4.html(位置在左上)





參考文獻:http://abgne.tw/jquery/apply-jquery/jquery-window-scroll-ad.html/comment-page-1?replytocom=3997

1 則留言:

  1. 你應該試著用 CSS position:fixed;
    這會更容易些!

    回覆刪除