图片横向滚动菜单

本文介绍了一种使用HTML和JavaScript实现图片滚动效果的方法。通过定义特定的样式和脚本,可以创建平滑滚动的图片展示区,并且加入了拖动条来方便用户操作。此方案适用于网页设计中需要动态展示大量图片的场景。

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

 

<!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> www.sd009.com</title
<style> 
ul{margin:0;padding:0;list-style:none;width:100000px} 
li{margin:0;padding:0;width:100px;height:100px;display:block;float:left;margin-right:5px;background:#009900} 
#img_bag{width:640px;height:100px;background:#FF9900;margin:0 auto;} 
#img_bag a{float:left;width:20px;display:block;height:20px;font-weight:bold;} 
#img_bag #img{width:600px;height:100px;background:#ccc;overflow:hidden;float:left} 
#scrollBar{width:600px;height:20px;background:#FF0000;margin:0 auto;position:relative} 
#scroll{width:30px;height:20px;background:#000;cursor:pointer;position:absolute;} 
</style> 
</head> 
<body> 
<div id="img_bag"> 
<a href="javascript:void(0)" onmousedown="moveLeft()"><<</a> 
<div id="img"> 
<ul> 
<li></li> 
<li></li> 
<li></li> 
<li></li> 
<li></li> 
<li></li> 
<li></li> 
<li></li> 
<li></li> 
<li></li> 
<li></li> 
<li></li> 
</ul> 
</div> 
<a href="javascript:void(0)" onmousedown="moveRight()">>></a> 
</div> 
<div style="clear:both;"></div> 
<div id="scrollBar"> 
<div id="scroll" style="left:0"></div> 
</div> 
<script> 
function $(obj){return document.getElementById(obj)} 
var maxWidth=$("img").getElementsByTagName("ul")[0].getElementsByTagName("li").length*105; 
var isScroll=false; 
var modiLeft; 
$("scroll").onmousedown=function(evt){ 
isScroll=true; 
evt=(evt)?evt:((window.event)?window.event:null); 
if(evt.offsetX){ 
modiLeft=parseInt(evt.offsetX) 

else{modiLeft=parseInt(evt.layerX)} 

document.onmouseup=function(){ 
isScroll=false; 

document.onmousemove=function(evt){ 
evt=(evt)?evt:((window.event)?window.event:null); 
if(evt&&isScroll){ 
$("scroll").style.left=parseInt(evt.clientX)-parseInt($("scrollBar").offsetLeft)-modiLeft+"px"; 
if(parseInt($("scroll").style.left)<0){$("scroll").style.left=0+"px"} 
if(parseInt($("scroll").style.left)>570){$("scroll").style.left=570+"px"} 
$("img").scrollLeft=parseInt($("scroll").style.left)*((maxWidth-600)/570); 
  } 

var targetx = 200;//一次滚动距离 
var dx; 
var a=null; 
function moveLeft(){ 
var le=parseInt($("img").scrollLeft); 
if(le>200){ 
targetx=parseInt($("img").scrollLeft)-200; 

else{targetx=parseInt($("img").scrollLeft)-le-1} 
scLeft(); 

function scLeft(){ 
dx=parseInt($("img").scrollLeft)-targetx; 
$("img").scrollLeft-=dx*.3; 
$("scroll").style.left=parseInt($("img").scrollLeft)*(570/(maxWidth-600))+"px"; 
if(parseInt($("scroll").style.left)<0){$("scroll").style.left=0+"px"} 
if(parseInt($("scroll").style.left)>570){$("scroll").style.left=570+"px"} 
clearScroll=setTimeout(scLeft,50); 
if(dx*.3<1){clearTimeout(clearScroll)} 

function moveRight(){ 
var le=parseInt($("img").scrollLeft)+200; 
var maxL=maxWidth-600; 
if(le<maxL){ 
targetx=parseInt($("img").scrollLeft)+200; 

else{targetx=maxL} 
scRight(); 

function scRight(){ 
dx=targetx-parseInt($("img").scrollLeft); 
$("img").scrollLeft+=dx*.3; 
$("scroll").style.left=parseInt($("img").scrollLeft)*(573/(maxWidth-600))+"px"; 
if(parseInt($("scroll").style.left)<0){$("scroll").style.left=0+"px"} 
if(parseInt($("scroll").style.left)>=570){$("scroll").style.left=570+"px"} 
a=setTimeout(scRight,50); 
if(dx*.3<1){clearTimeout(a)} 

</script> 
</body> 
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值