js通知公告文字左右轮播滚动

js通知公告文字左右轮播滚动

在这里插入图片描述

HTML结构

<div class="llbr_01">
  <div><a href="#" target="_blank" style="color:red;">英雄</a></div>
  <div id="marquee1" style="width:468px;height: 38px;float: left;overflow: hidden;">
    <div style="width:8000px;height: 38px; background:none;margin-left: 0;">
      <ul id="marquee1_1" style="float:left;">
        <li style="float: left; width: 68px; text-align: center; height: 38px;margin-top: 0;"><a href="#" target="_blank" style="color: #333; font-size:16px;">石头人</a></li>
        <li style="float: left; width: 68px; text-align: center; height: 38px;margin-top: 0;"><a href="#" target="_blank" style="color: #333; font-size:16px;">小鱼人</a></li>
        <li style="float: left; width: 68px; text-align: center; height: 38px;margin-top: 0;"><a href="#" target="_blank" style="color: #333; font-size:16px;">亚索</a></li>
        <li style="float: left; width: 68px; text-align: center; height: 38px;margin-top: 0;"><a href="#" target="_blank" style="color: #333; font-size:16px;">炸弹人</a></li>
        <li style="float: left; width: 68px; text-align: center; height: 38px;margin-top: 0;"><a href="#" target="_blank" style="color: #333; font-size:16px;">女枪</a></li>
        <li style="float: left; width: 68px; text-align: center; height: 38px;margin-top: 0;"><a href="#" target="_blank" style="color: #333; font-size:16px;">奥拉夫</a></li>
        <li style="float: left; width: 68px; text-align: center; height: 38px;margin-top: 0;"><a href="#" target="_blank" style="color: #333; font-size:16px;">盲僧</a></li>
        <li style="float: left; width: 68px; text-align: center; height: 38px;margin-top: 0;"><a href="#" target="_blank" style="color: #333; font-size:16px;">阿卡丽</a></li>
        <li style="float: left; width: 68px; text-align: center; height: 38px;margin-top: 0;"><a href="#" target="_blank" style="color: #333; font-size:16px;">机器人</a></li>
        <li style="float: left; width: 68px; text-align: center; height: 38px;margin-top: 0;"><a href="#" target="_blank" style="color: #333; font-size:16px;">德莱文</a></li>
      </ul>
      <ul id="marquee1_2" style="float:left;">
      </ul>
    </div>
  </div>
</div>

CSS样式

<style>
* {
	margin: 0;
	padding: 0;
}
body {
	font-family: "microsoft yahei", "宋体";
	color: #333;
	font-size: 16px;
	background-position: 0px 170px
}
li {
	list-style: none;
}
input, button, img {
	outline: none;
	border: none;
}
a, a:visited, a:link {
	text-decoration: none;
	color: #333;
}
a:hover {
	color: #333;
	text-decoration: none;
}
.llbr_01{
	width: 800px;
    margin: 0 auto;
	}

</style>

JS滚动代码

<script type="text/javascript">
//js无缝滚动代码
function marquee(i, direction){
	var obj = document.getElementById("marquee" + i);
	var obj1 = document.getElementById("marquee" + i + "_1");
	var obj2 = document.getElementById("marquee" + i + "_2");
	if (direction == "up"){
		if (obj2.offsetTop - obj.scrollTop <= 0){
			obj.scrollTop -= (obj1.offsetHeight + 20);
		}else{
			var tmp = obj.scrollTop;
			obj.scrollTop++;
			if (obj.scrollTop == tmp){
				obj.scrollTop = 1;
			}
		}
	}else{
		if (obj2.offsetWidth - obj.scrollLeft <= 0){
			obj.scrollLeft -= obj1.offsetWidth;
		}else{
			obj.scrollLeft++;
		}
	}
}

function marqueeStart(i, direction){
	var obj = document.getElementById("marquee" + i);
	var obj1 = document.getElementById("marquee" + i + "_1");
	var obj2 = document.getElementById("marquee" + i + "_2");

	obj2.innerHTML = obj1.innerHTML;
	var marqueeVar = window.setInterval("marquee("+ i +", '"+ direction +"')", 30);
	obj.onmouseover = function(){
		window.clearInterval(marqueeVar);
	}
	obj.onmouseout = function(){
		marqueeVar = window.setInterval("marquee("+ i +", '"+ direction +"')", 30);
	}
}
</script>
<script type="text/javascript">marqueeStart(1, "left");</script> 

效果展示

在这里插入图片描述

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

北斗星的爱886

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

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

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

打赏作者

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

抵扣说明:

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

余额充值