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

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

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文字橫向滾動(dòng)</span><span>jQuery制作文字橫向滾動(dòng)</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文字橫向滾動(dòng)效果的實(shí)現(xiàn)代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:jQuery文字橫向滾動(dòng)效果的實(shí)現(xiàn)代碼
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢(xún)回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢(xún)?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽(tīng)報(bào)名

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