網站右下角常見的「抖動浮動」在線客服或廣告圖,能吸引目光又不過度干擾。本文示範如何用 jQuery 實作一個固定在右下角的浮動圖,並加上週期性抖動效果,同時相容 IE6 與 Firefox。
setTimeout("document.getElementById('qq_dd').style.display='block'; ",5000);
// JavaScript Document
$(function(){
if($.browser.msie&&$.browser.version=="6.0"&&$("html")[0].scrollHeight>$("html").height())
$("html").css("overflowY","scroll");
$("html").css("overflowX","hidden");
});
function right_down(){
document.writeln("<div id='qq_dd' class='foot_scroll' style='display:none;position:fixed; margin:3px;bottom:3px; right:5px;z-index:1000;_position:absolute; _top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.marginTop,10)||0)-(parseInt(this.currentStyle.marginBottom,10)||0)));_bottom:auto;'><div id='my_img' style='position:relative; *left:550px; width:214px; height:124px; font-size:12px; color:#1670eb; background:url(online_cn_fk.png) no-repeat;'><img src='closed_fk.jpg' style='float:right; cursor:pointer;' id='guanbi'onclick=\"_gaq.push(['_trackEvent', 'QQDD', 'Click', '關閉']);\" /><a href='#' target='_blank' style='display:block; margin:97px 0 0 100px; display:inline; float:left; color:#1670eb;'><img src='jieshou_fk.jpg' onclick=\"_gaq.push(['_trackEvent', 'QQDD', 'Click', '咨詢']);\" border='0'/></a><span style='cursor:pointer; float:right; margin:74px 35px 0 0; display:inline;' id='guanbi0'><img src='jujue_fk.jpg' onclick=\"_gaq.push(['_trackEvent', 'QQDD', 'Click', '關閉']);\" /></span></div></div>")
}
right_down();
$("#guanbi").click(function(){
$("#my_img").hide();
});
$("#guanbi0").click(function(){
$("#my_img").hide();
});
function rattleimage(){$("#my_img").animate({top:'+=3'},50);$("#my_img").animate({left:'+=3'},50);$("#my_img").animate({top:'-=3'},50);$("#my_img").animate({left:'-=3'},50);$("#my_img").animate({top:'+=3'},50);$("#my_img").animate({left:'+=3'},50);$("#my_img").animate({top:'-=3'},50);$("#my_img").animate({left:'-=3'},50);$("#my_img").animate({top:'+=3'},50);$("#my_img").animate({left:'+=3'},50);$("#my_img").animate({top:'-=3'},50);$("#my_img").animate({left:'-=3'},50);$("#my_img").animate({top:'+=3'},50);$("#my_img").animate({left:'+=3'},50);$("#my_img").animate({top:'-=3'},50);$("#my_img").animate({left:'-=3'},50);$("#my_img").animate({top:'+=3'},50);$("#my_img").animate({left:'+=3'},50);$("#my_img").animate({top:'-=3'},50); $("#my_img").animate({left:'-=3'},50);$("#my_img").fadeOut("slow");$('#my_img').fadeIn('slow');setTimeout("rattleimage()",10000);}
rattleimage();
整個效果分幾個部分:先用 setTimeout 讓浮動層在頁面載入 5 秒後顯示;透過 CSS 的 position:fixed 把區塊固定在右下角,並用 _position:absolute 搭配 _top:expression(...) 做 IE6 的相容處理。抖動效果則由 rattleimage() 連續呼叫多次 animate(),讓圖片上下左右小幅位移,模擬「抖動」的吸睛效果,最後用 setTimeout 每 10 秒循環一次。同時綁定兩個關閉按鈕的點擊事件,讓使用者能隨時把浮動圖隱藏。