web前端js基础------制作滚动图片

1,要求

通过定时器使其出现滚动的效果

可以通过按键控制图片滚动的方向(设置两个按钮绑定点击事件)

当鼠标悬停时图片停止,鼠标离开时图片继续向前滚动(可以设置鼠标的悬停和离开事件)

参考如下

html 复制代码
			content.onmouseenter = function(){//设置鼠标悬停事件
				key = "stop";
			}
			content.onmouseleave = function(){//设置鼠标离开事件
				key = "run";
			}

图片可以持续滚动,不会出现空白(添加滚动图片)

2,参考代码(左右滚动)

html 复制代码
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
			#slide{
				width:800px;
				height: 100px;
				margin: 0 auto;
				margin-top: 100px;
				border: 1px solid black;
				overflow: hidden;
			}
			#content{
				width: 999999px;
			}
			img{
				width: 160px;
				height: 100px;
				float: left;
				margin-right: 10px;
				cursor: pointer;
			}
			#button{
				width: 100px;
				margin: 20px auto;
			}
		</style>
	</head>
	<body>
		<div id="slide">
			<div id="content">
				
				<img src="../img/1.png"/>
				<img src="../img/2.png"/>
				<img src="../img/3.png"/>
				<img src="../img/4.png"/>
				<img src="../img/5.png"/>
				<img src="../img/6.png"/>
				<img src="../img/7.png"/>
				<img src="../img/8.png"/>
				<img src="../img/9.png"/>
			</div>
		</div>
		<div id="button">
				<button type="button" id="left">向左</button>
				<button type="button" id="right">向右</button>
			</div>
		<script type="text/javascript">
			var content = document.getElementById("content");
			var left = document.getElementById("left");
			var right = document.getElementById("right");
			content.style.marginLeft = 0+"px";
			content.innerHTML = content.innerHTML+content.innerHTML+content.innerHTML;
			var key = "run";
			var dkey = "left";
			left.onclick = function(){
				dkey = "left";
			}
			right.onclick = function(){
				dkey = "right";
			}
			content.onmouseenter = function(){
				key = "stop";
			}
			content.onmouseleave = function(){
				key = "run";
			}
			dd();
			function dd(){
				var num = parseFloat(content.style.marginLeft);
				if(key == "run"){
					if(dkey == "left"){
						num-=0.5;
						if(num<=-170*9){
							num=0;
						}
					}else{
						num+=0.5;
						if(num>=0){
							num=-170*9;
						}
					}
				}
				content.style.marginLeft = num+"px";
				setTimeout("dd()",13);
			}
		</script>
	</body>
</html>

3,结果参考示例

4,参考代码(上下滚动)

html 复制代码
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			/* 设置盒子 */
			#div{
				height: 800px;
				width: 100px;
				border: 1px solid black;
				margin: 0px auto;
				overflow: hidden;			
			}
			/* 设置内容即图片的容器*/
			#content{
				height: 999999px;
			}
			/* 设置图片的样式 */
			img{
				width: 100px;
				height: 160px;
				margin-top: 10px;
				cursor: pointer;
			}
			/* 设置按钮样式*/
			#button{ 
				width: 100px;
				margin: 20px auto;
			}
		</style>
	</head>
	<body>
		<div id="div">
			<div id="content">
				<img src="../img/2ee18-231100-163232346010b0.jpg"/>
				<img src="../img/1.png"/>
				<img src="../img/2.png"/>
				<img src="../img/3.png"/>
				<img src="../img/4.png"/>
				<img src="../img/5.png"/>
				<img src="../img/6.png"/>
				<img src="../img/7.png"/>
				<img src="../img/8.png"/>
				<img src="../img/9.png"/>
			</div>
		</div>
		<div id="button">
				<button type="button" id="top">向上</button>
				<button type="button" id="buttom">向下</button>
		</div>
		<script>
			var content = document.getElementById("content");//获取内容的id
			var topSlide =document.getElementById("top");//获取向上按钮的id
			var downSlide =document.getElementById("buttom");//获取向下按钮的id
			content.innerHTML = content.innerHTML+content.innerHTML+content.innerHTML;//使内容乘于3倍
			content.style.marginTop = 0 + "px";//设置外边距初始值
			var key = "run";//设置初始是运行的
			var dkey = "top";//设置初始是向右边的
			topSlide.onclick = function(){
				dkey = "top";
			}
			downSlide.onclick = function(){
				dkey = "buttom";
			}
			content.onmouseenter = function(){//设置鼠标悬停事件
				key = "stop";
			}
			content.onmouseleave = function(){//设置鼠标离开事件
				key = "run";
			}
			
			slide();
			function slide(){
				var num = parseFloat(content.style.marginTop);
			if(key == "run"){
				if(dkey == "top"){
					num-=0.5;
					if(num<=-170*9){
						num=0;
					}
				}else{
					num+=0.5;
					if(num>=0){
						num=-170*9;
					}
				}
			}		
				content.style.marginTop = num+"px";
				setTimeout("slide()",13);//设置定时器使其运行
			}
		</script>
	</body>
</html>

效果同3

相关推荐
ascarl20103 分钟前
准确--k8s cgroup问题排查
java·开发语言
前端小趴菜054 分钟前
React - 组件通信
前端·react.js·前端框架
Amy_cx24 分钟前
在表单输入框按回车页面刷新的问题
前端·elementui
dancing99939 分钟前
cocos3.X的oops框架oops-plugin-excel-to-json改进兼容多表单导出功能
前端·javascript·typescript·游戏程序
fpcc1 小时前
跟我学c++中级篇——理解类型推导和C++不同版本的支持
开发语言·c++
莱茵菜苗1 小时前
Python打卡训练营day46——2025.06.06
开发语言·python
爱学习的小道长1 小时前
Python 构建法律DeepSeek RAG
开发语言·python
后海 0_o1 小时前
2025前端微服务 - 无界 的实战应用
前端·微服务·架构
Scabbards_1 小时前
CPT304-2425-S2-Software Engineering II
前端
小满zs1 小时前
Zustand 第二章(状态处理)
前端·react.js