1. 程式人生 > 其它 >js通知公告文字左右輪播滾動

js通知公告文字左右輪播滾動

技術標籤:javascripthtml5css3jqueryes6

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> 

效果展示

在這裡插入圖片描述