欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

jQuery实现文字横向滚动效果 有大用

jQuery实现文字横向滚动效果

HTML代码:

1
2
3
4
5
<div id="aaa" 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>
</div> 
</div>

Jquery代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$.fn.textScroll=function(){ 
        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(falsefalse); 
            clearInterval(f);
            console.log(t1); 
        },  mouseleave: function () {
            ani(t - t1); 
            console.log(t1);
        }  }); 
        ani(t);  }

  页面调用:

1
2
3
$(function(){
$("#aaa").textScroll();//aaa为最外层div的Id  也可写Class  如:$(".aaa") 此处aaa为class名
});

  

 

分类: JS


来自   https://www.cnblogs.com/liuyanpeng/p/6589680.html

普通分类: