图片向左滚动或文字向左滚动代码,无缝滚动且划过停止

本文介绍如何使用纯JavaScript实现文字和图片的无缝滚动效果,并在鼠标悬停时停止滚动。通过调整速度变量可以控制滚动的速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文主要讲解运用相同的js代码,使图片向左滚动或文字向左滚动
文字向左滚动,无缝滚动且划过停止,纯javascript。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
<!--
#demo {
background: #FFF;
overflow:hidden;
border: 1px dashed #CCC;
width: 500px;
font-size: 12px;
height: 30px;
line-height: 30px;
padding: 5px;
}
#demo a{
color:#333;
}
#indemo {
float: left;
width: 800%;
}
#demo1 {
float: left;
}
#demo2 {
float: left;
}
-->
</style>
</head>

<body>
向左滚动
<div id="demo">
<div id="indemo">
<div id="demo1">
<a href="http://www.goonedu.com/Colleges/School.aspx?SchoolID=231">山东大学</a>
<a href="http://www.goonedu.com/Colleges/School.aspx?SchoolID=205">中北大学</a>
<a href="http://www.goonedu.com/Colleges/School.aspx?SchoolID=377">华中科技大学</a>
<a href="http://www.goonedu.com/Colleges/School.aspx?SchoolID=333">西安工程大学</a>
<a href="http://www.goonedu.com/Colleges/School.aspx?SchoolID=359">西北工业大学</a>
<a href="http://www.goonedu.com/Colleges/School.aspx?SchoolID=91">西南民族大学</a>
<a href="http://www.goonedu.com/Colleges/School.aspx?SchoolID=246">中国海洋大学</a>
<a href="http://www.goonedu.com/Colleges/School.aspx?SchoolID=64">中国科技大学</a>
</div>
<div id="demo2"></div>
</div>
</div>

<script>
<!--
var speed=10; //数字越大速度越慢
var tab=document.getElementById("demo");
var tab1=document.getElementById("demo1");
var tab2=document.getElementById("demo2");
tab2.innerHTML=tab1.innerHTML;
function Marquee(){
if(tab2.offsetWidth-tab.scrollLeft<=0)
tab.scrollLeft-=tab1.offsetWidth
else{
tab.scrollLeft++;
}
}
var MyMar=setInterval(Marquee,speed);
tab.onmouseover=function() {clearInterval(MyMar)};
tab.onmouseout=function() {MyMar=setInterval(Marquee,speed)};
-->
</script>

</body>
</html>


图片向左滚动,无缝滚动且划过停止,纯javascript。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<div id="colee_left" style="overflow:hidden;width:920px; margin:0px; padding:0px; height:142px;">
<table cellpadding="0" cellspacing="0" border="0">
<tr><td id="colee_left1" valign="top" align="center">
<table cellpadding="3" cellspacing="0" border="0">
<tr align="center">
<td><div class="kuang">
<div class="photo_top"></div>
<div class="photo_pic"><a href="#"><img src="images/lzp.jpg" width="169" height="116" /></a></div>
<div class="photo_name"><a href="#">老照片</a></div>
</div></td>
<td><div class="kuang">
<div class="photo_top"></div>
<div class="photo_pic"><a href="#"><img src="images/lzp.jpg" width="169" height="116" /></a></div>
<div class="photo_name"><a href="#">老照片</a></div>
</div></td>
<td><div class="kuang">
<div class="photo_top"></div>
<div class="photo_pic"><img src="images/lzp.jpg" width="169" height="116" /></div>
<div class="photo_name"><a href="#">老照片</a></div>
</div></td>
<td><div class="kuang">
<div class="photo_top"></div>
<div class="photo_pic"><img src="images/lzp.jpg" width="169" height="116" /></div>
<div class="photo_name"><a href="#">老照片</a></div>
</div></td>
<td><div class="kuang">
<div class="photo_top"></div>
<div class="photo_pic"><img src="images/lzp.jpg" width="169" height="116" /></div>
<div class="photo_name"><a href="#">老照片</a></div>
</div></td>

</tr>
</table>
</td>
<td id="colee_left2" valign="top"></td>
</tr>
</table>
</div>
<script>
//使用div时,请保证colee_left2与colee_left1是在同一行上.
var speed=30//速度数值越大速度越慢
var colee_left2=document.getElementById("colee_left2");
var colee_left1=document.getElementById("colee_left1");
var colee_left=document.getElementById("colee_left");
colee_left2.innerHTML=colee_left1.innerHTML
function Marquee3(){
if(colee_left2.offsetWidth-colee_left.scrollLeft<=0)//offsetWidth 是对象的可见宽度
colee_left.scrollLeft-=colee_left1.offsetWidth//scrollWidth 是对象的实际内容的宽,不包边线宽度
else{
colee_left.scrollLeft++
}
}
var MyMar3=setInterval(Marquee3,speed)
colee_left.onmouseover=function() {clearInterval(MyMar3)}
colee_left.onmouseout=function() {MyMar3=setInterval(Marquee3,speed)}
</script>

</body>
</html>


本博客陆续推出,多种滚动特效,欢迎访问!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

e421083458

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

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

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

打赏作者

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

抵扣说明:

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

余额充值