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

相关推荐
懒得不想起名字几秒前
flutter_riverpod: ^2.6.1 应用笔记
前端
CrabXin几秒前
让网页在 PC 缩放时“纹丝不动”的 4 个技巧
前端·react.js
@半良人8 分钟前
Deepseek+python自动生成禅道测试用例
开发语言·python·测试用例
Juchecar11 分钟前
Naive UI 学习指南 - Vue3 初学者完全教程
前端·vue.js
用户81686947472512 分钟前
从0到1教你开发一个Mini-ESLint
前端·开源
coding随想12 分钟前
JavaScript中的DOM事件对象Event全解析
前端
专研狂12 分钟前
React 的闭包陷阱 + 状态异步更新机制
前端
一只鲲16 分钟前
48 C++ STL模板库17-容器9-关联容器-映射(map)多重映射(multimap)
开发语言·c++
zabr17 分钟前
AI黑箱解密:开发者必须了解的AI内部机制真相,原来我们一直被忽悠了
前端·aigc·ai编程
Sokach38629 分钟前
vue3引入tailwindcss 4.1
前端·css