zoukankan      html  css  js  c++  java
  • jQuery自定义滚动文字函数

    文字滚动本身是可以通过html的<marquee>标签来实现的,但是滚动方式不够灵活,有时候无法满足需求。于是自己动手写了个jQuery插件,本身没什么难度,非常简单。

    $.fn.extend({
    		scrollTxt:function(speed){
    			var parentObj = this.parent();
    			var _this = this;
    			clearInterval(this.timer);
    		    if(this.width()> parentObj.width()){
    		    	this.timer = setInterval(function(){
    		    			var left = _this.css("left");
    		  				left = left.replace(new RegExp('px'), "");
    				    	if(-left < _this.width()){
    				    		var nowLeft = _this.css("left");
    				    		nowLeft = nowLeft.replace(new RegExp('px'), "");
    		    				_this.css("left",--nowLeft);
    						}else{
    		    				_this.css("left",(parentObj.width()));
    		    			}
    		    		},speed?speed:30);
    		    }
    		}
    	})
    

      HTML部分

    <div id="scrollTxt">
        <p>要滚动的文字</p>
    </div>

    然后执行$("#scrollTxt p").scrollTxt();就可以实现文字滚动效果了

    如果无法实现,可能是CSS样式的问题:

    #scrollTxt{ 
        width: 800px;
        overflow: hidden;
    }
    #scrollTxt p{
        display: inline-block;
        white-space: nowrap;
        position: relative;
        left: 0; 
    }
  • 相关阅读:
    并发编程之进程池,线程池 和 异步回调,协程
    form与modeform
    5个_meta方法
    CRM项目知识预备
    Jason数据库查询语句
    kindeditor编辑器
    几种单例模式
    BBS项目复习
    BBS项目小组件
    BBS项目附加知识
  • 原文地址:https://www.cnblogs.com/flappyCoder/p/4018660.html
Copyright © 2011-2022 走看看