jq文字无缝滚动代码(鼠标悬停停止滚动)

jq实现文字无缝滚动代码(鼠标悬停停止滚动)

效果图
这里写图片描述


直接copy就能运行

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>文字无缝滚动代码鼠标悬停停止滚动</title>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
(function($){
	$.fn.myScroll = function(options){
	//默认配置
	var defaults = {
		speed:40,  //滚动速度,值越大速度越慢
		rowHeight:24 //每行的高度
	};
	var opts = $.extend({}, defaults, options),intId = [];	
	function marquee(obj, step){	
		obj.find("ul").animate({
			marginTop: '-=1'
		},0,function(){
				var s = Math.abs(parseInt($(this).css("margin-top")));
				if(s >= step){
					$(this).find("li").slice(0, 1).appendTo($(this));
					$(this).css("margin-top", 0);
				}
			});
		}		
		this.each(function(i){
			var sh = opts["rowHeight"],speed = opts["speed"],_this = $(this);
			intId[i] = setInterval(function(){
				if(_this.find("ul").height()<=_this.height()){
					clearInterval(intId[i]);
				}else{
					marquee(_this, sh);
				}
			}, speed);
			_this.hover(function(){
				clearInterval(intId[i]);
			},function(){
				intId[i] = setInterval(function(){
					if(_this.find("ul").height()<=_this.height()){
						clearInterval(intId[i]);
					}else{
						marquee(_this, sh);
					}
				}, speed);
			});
		
		});
	}
})(jQuery);
</script>
<style>
	*{margin:0;padding:0;}
	ul,li{list-style:none;}
	a{color:#666;text-decoration:none;}
	.bcon{width:200px;border:1px solid #eee;margin:30px auto;}
	.list_lh{height:30px;overflow:hidden;text-align: center;font-size: 16px}
	.list_lh li p{height:30px;line-height:30px;}
</style>
<script type="text/javascript">
	$(document).ready(function(){		
		$("div.list_lh").myScroll({
			speed:40, //数值越大,速度越慢
			rowHeight:30 //li的高度
		});
	})
</script>
</head>

<body>
<div class="bcon">
	<div class="list_lh">
		<ul>
			<li><p>1号获得 iphone X</p></li>
			<li><p>2号获得 iphone 8</p></li>
			<li><p>3号获得 iphone 7</p></li>
			<li><p>4号获得 iphone 6</p></li>
			<li><p>5号获得 iphone 5</p></li>
			<li><p>6号获得 iphone 5</p></li>
			<li><p>7号获得 iphone 6</p></li>
			<li><p>8号获得 iphone 7</p></li>
			<li><p>9号获得 iphone 8</p></li>
			<li><p>0号获得 iphone X</p></li>
		</ul>
	</div>
</div>

</body>
</html>
posted @ 2022-07-20 18:17  猫老板的豆  阅读(486)  评论(0编辑  收藏  举报