js,jq单行文字上下滚动

 js,jq单行文字上下滚动,方法记录

页面布局什么没什么新奇 只要保证只显示一行文字就好 如果需要显示多行 自行设置高度,这都不重要

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
		<meta http-equiv="X-UA-Compatible" content="ie=edge">
		<title>demo</title>
		<style type="text/css">
			* {
				padding: 0;
				margin: 0;
				list-style: none;
				text-decoration: none;
			}
			
			.box {
				width: 100%;
				position: relative;
				background: #ef2d68;
				padding: 20px 0;
			}
			
			.roll_box {
				position: relative;
				font-size: 30px;
				height: 40px;
				color: #fff;
				font-weight: bold;
				overflow: hidden;
				text-align: center;
			}
		</style>
	</head>

	<body>
		<div class="box">
			<div class="roll_box">
				<ul id="roll">
					<li>单行文字上下滚动__1</li>
					<li>单行文字上下滚动__2</li>
					<li>单行文字上下滚动__3</li>
					<li>单行文字上下滚动__4</li>
					<li>单行文字上下滚动__5</li>
				</ul>
			</div>
		</div>
	</body>
	<script type="text/javascript">
		roll('roll', 3000)

		function roll(elem, time) {
            //js 方法
			var ul = document.getElementById(elem)
			var height = ul.getElementsByTagName('li')[0].offsetHeight;
			setInterval(function() {
				domAnimate(elem).animate({
					"margin-top": -height + 'px'
				}, 1000, function() {
					ul.style.marginTop = "0px";
					ul.appendChild(ul.getElementsByTagName('li')[0])
				})
			}, time || 5000)

           // JQ 方法  使用JQ的话需要引入JQ包
//			var ul = $("#"+elem);
//			var li = ul.find("li");
//			ul.animate({
//				"margin-top": -li.innerHeight() + 'px'
//			}, 500, function() {
//				ul.css({
//					marginTop: "0px"
//				}).find("li:first").appendTo(ul);
//			})
		}
         //该方法为仿JQ的animate() 方法
		function domAnimate(id) {
			var elem = document.getElementById(id),
				f = j = 0,
				callback, _this = {},
				tween = function(t, b, c, d) {
					return -c * (t /= d) * (t - 2) + b
				}
			_this.execution = function(key, val, t) {
				var s = (new Date()).getTime(),
					d = t || 500,
					b = parseInt(elem.style[key]) || 0,
					c = val - b;
				(function() {
					var t = (new Date()).getTime() - s;
					if(t > d) {
						t = d;
						elem.style[key] = tween(t, b, c, d) + 'px';
						++f == j && callback && callback.apply(elem);
						return _this;
					}
					elem.style[key] = tween(t, b, c, d) + 'px';
					setTimeout(arguments.callee, 10);
				})();
			}
			_this.animate = function(sty, t, fn) {
				callback = fn;
				for(var i in sty) {
					j++;
					_this.execution(i, parseInt(sty[i]), t);
				}
			}
			return _this;
		}
	</script>

</html>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

招来红月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值