jQuery文字橫向滾動效果的實現(xiàn)代碼
來源:易賢網(wǎng) 閱讀:4121 次 日期:2016-06-17 15:51:24
溫馨提示:易賢網(wǎng)小編為您整理了“jQuery文字橫向滾動效果的實現(xiàn)代碼”,方便廣大網(wǎng)友查閱!

很多網(wǎng)站都有公告,并且都喜歡用橫向滾動的方式來展現(xiàn)。最近自己用jQuery也寫了文字橫向滾動,實現(xiàn)了自定義滾動速度,鼠標經(jīng)過時暫停的功能。

HTML代碼如下:

代碼如下:

<div id="s" style="width:100px; position:relative; white-space:nowrap; overflow:hidden; height:20px;">

<div id="noticeList" style="position:absolute; top:0; height:20px;"><span>jQuery文字橫向滾動</span><span>jQuery制作文字橫向滾動</span><span>www.jb51.net</span></div>

</div>

JS代碼:

方法一:

$.fn.textScroll=function(){

var speed=60,flag=null,tt,that=$(this),child=that.children();

var p_w=that.width(), w=child.width();

child.css({left:p_w});

var t=(w+p_w)/speed * 1000;

function play(m){

var tm= m==undefined ? t : m;

child.animate({left:-w},tm,"linear",function(){

$(this).css("left",p_w);

play();

});

}

child.on({

mouseenter:function(){

var l=$(this).position().left;

$(this).stop();

tt=(-(-w-l)/speed)*1000;

},

mouseleave:function(){

play(tt);

tt=undefined;

}

});

play();

}

方法二:

$.fn.textScroll=functioon(){

var p = $(this),

c = p.children(),

speed=3000;// 值越大,速度越小

var cw = c.width(),pw=p.width();

var t = (cw / 100) * speed;

var f = null, t1 = 0;

function ani(tm) {

counttime();

c.animate({ left: -cw }, tm, "linear", function () {

c.css({ left: pw });

clearInterval(f);

t1 = 0;

t=((cw+pw)/100)*speed;

ani(t);

});

}

function counttime() {

f = setInterval(function () {

t1 += 10;

}, 10);

}

p.on({

mouseenter: function () {

c.stop(false, false);

clearInterval(f);

console.log(t1);

},

mouseleave: function () {

ani(t - t1);

console.log(t1);

}

});

ani(t);

}

最后調(diào)用$("#s").textScroll();

以上這篇jQuery文字橫向滾動效果的實現(xiàn)代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機網(wǎng)站地址:jQuery文字橫向滾動效果的實現(xiàn)代碼

2026國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)