老师我的效果出不来?
//封装getElementById()
function byId(id){
return typeof(id)==="String"?document.getElementById('id'):id;
}
var
timer=null,
pics=byId("banner").getElementsByTagName('div'),
len=pics.length,
index=0;
function slideImg(){
//取出mian元素
var main=byId("main");
//打开页面自动执行图片轮播
main.onmouseout();
//鼠标放置在图片上
// main.onmouseover=function(){
// if(timer)
// clearInterval(timer);
// }
//鼠标从图片上移走时
main.onmouseout=function(){
timer= setInterval(function(){
index++;
if(index==len)
index=0;
//切换图片
changeimg();
},2000);
}
}
//i切换图片
changeimg(){
//遍历banner里所有的div,将其隐藏
for(var i=0;i<len;i++)
pics[i].style.display="none";
//根据索引找到当前div,将其显示出来
pics[index].style.display='block';
}
slideImg();
我先写了鼠标离开图片时的事件,可是没效果
正在回答 回答被采纳积分+1
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>综合案例</title>
<link rel="stylesheet" type="text/css" href="综合案例.css">
<body>
<div class="main" id="main">
<div class="banner" id="banner">
<a href="">
<div class="banner-slide slide1 slide-active"></div>
</a>
<a href="">
<div class="banner-slide slide2 "></div>
</a>
<a href="">
<div class="banner-slide slide3"></div>
</a>
</div>
<a href="javascript:void(0)" class=" button prev" id="prev"></a>>
<a href="javascript:void(0)" class=" button next" id="next"></a>
<div class="dots" id="dots">
<span class="active"></span>
<span></span>
<span></span>
</div>
</div>
<script src="myself.js">
</script>
</body>
</html>
css代码:
*{
margin: 0;
padding: 0;
}
body{
font-family: "微软雅黑";
color: #14191e;
}
.main{
width: 1200px;
height: 460px;
overflow: hidden;
margin: 30px auto;
position: relative;
}
.banner{
width: 1200px;
height: 460px;
overflow: hidden;
position: relative;
}
.banner-slide{
width: 1200px;
height: 460px;
background-repeat: no-repeat;
position: absolute;
display: none;
}
.slide1{
background-image:url(img\\bg1.jpg);
}
.slide2{
background-image:url(img\\bg2.jpg);
}
.slide3{
background-image:url(img\\bg3.jpg);
}
.slide-active{
display: block;
}
.button{
position: absolute;
width: 40px;
height: 80px;
left: 244px;
top: 50%;
margin-top: -40px;
background: url("img\\arrow.png") no-repeat center center;
}
.prev{
transform: rotate(180deg);
}
.next{
right: 0;
left: auto;
}
.button:hover{
background-color: #333;
opacity: 0.5;
}
.dots{
position: absolute;
bottom: 20px;
right: 0;
text-align: right;
margin-right: 30px;
}
span{
width: 12px;
height: 12px;
border-radius: 50%;
line-height: 12px;
background-color:black;
opacity: 0.4;
background:rgba(7,17,27,0.4);
/*添加阴影*/
box-shadow: 0 0 0 1px rgba(255,255,255,0.8) inset;
display: inline-block;
margin-left: 10px;
/*光标形状*/
cursor: pointer;
}
.active{
background: #fff;
box-shadow: 0 0 0 1px rgba(7,17,27,0.4) inset;
}
js代码:
var index=0,
timer=null,
pics= byId("banner").getElementsByTagName("div"),
dots= byId("dots").getElementsByTagName('span'),
prev= byId("prev"),
next= byId("next"),
len =pics.length;
//封装getElementById()
function byId(id){
return typeof(id)==="String"?document.getElementById(id):id;
}
function slideImg(){
//取出mian元素
var main=byId("main");
//鼠标放置在图片上, 清除定时器,停止自动播放
main.onmouseover=function(){
if(timer){
clearInterval(timer);
}
}
//鼠标从图片上移走时,调用onmouseout事件
main.onmouseout=function(){
timer = setInterval(function(){
index++;
if(index >= len){
index = 0;
}
changeImg();
},3000);
}
//打开页面自动执行图片轮播,调用onmouseout方法
main.onmouseout();
//遍历所有点击,且绑定点击事件,点击圆点切换图片
for(var d=0;d<len;d++){
//给所有span添加一个id的属性,值为d,作为当前span的索引
dots[d].id=d;
dots[d].onclick=function(){
index=this.id;
changeImg();
}
}
//下一张
next.onclick=function(){
index++;
if (index>=len)
{
index=0;
}
changeImg();
}
//上一张
prev.onclick=function(){
index--;
if (index<0)
{
index=len-1;
}
changeImg();
}
}
//切换图片
function changeImg(){
//遍历banner里所有的div,将其隐藏
for(var i=0;i<len;i++)
{
pics[i].style.display="none";
dots[i].className="";
}
//根据索引找到当前div,将其显示出来
pics[index].style.display="block";
docs[index].className="active";
}
slideImg();
var index=0,
timer=null,
pics= byId("banner").getElementsByTagName("div"),
dots= byId("dots").getElementsByTagName('span'),
prev= byId("prev"),
next= byId("next"),
len =pics.length;
//封装getElementById()
function byId(id){
return typeof(id)==="String"?document.getElementById(id):id;
}
//切换图片
function changeImg(){
//遍历banner里所有的div,将其隐藏
for(var i=0;i<len;i++)
{
pics[i].style.display="none";
dots[i].className="";
}
//根据索引找到当前div,将其显示出来
pics[index].style.display='block';
docs[index].className="active";
}
function slideImg(){
//取出mian元素
var main=byId("main");
//鼠标放置在图片上
main.onmouseover=function(){
if(timer)
clearInterval(timer);
}
//鼠标从图片上移走时,调用onmouseout事件
main.onmouseout=function(){
timer= setInterval(function(){
index++;
if(index>=len)
index=0;
//切换图片
changeImg();
},2000);
}
//打开页面自动执行图片轮播,调用onmouseout方法
main.onmouseout();
//遍历所有点击,且绑定点击事件,点击圆点切换图片
for(var d=0;d<len;d++)
{
//给所有span添加一个id的属性,值为d,作为当前span的索引
dots[d].id=d;
dots[d].onclick=function(){
index=this.id;
changeImg();
}
}
//下一张
next.onclick=function(){
index++;
if (index>=len)
{
index=0;
}
changeImg();
}
//上一张
prev.onclick=function(){
index--;
if (index<0)
{
index=len-1;
}
changeImg();
}
}
slideImg();
这是帮助你修改后的代码,建议同学对照一下:
HTML页面:
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>Document</title> <link rel="stylesheet" href="css/style.css"> </head> <body> <div class="main" id="main"> <!-- 菜单 --> <div class="banner" id="banner"> <a href=""> <div class="banner-slide slide1 slide-active"></div> </a> <a href=""> <div class="banner-slide slide2"></div> </a> <a href=""> <div class="banner-slide slide3"></div> </a> </div> <a href="javascript:void(0)" class="button next" id="next"></a> <div class="dots" id="dots"> <span class="active"></span> <span></span> <span></span> </div> </div> <script src="js/script.js"></script> </body> </html>
script.js页面:
var timer = null, index = 0, pics = byId("banner").getElementsByTagName("div"), dots = byId("dots").getElementsByTagName("span"), size = pics.length function byId(id){ return typeof(id)==="string"?document.getElementById(id):id; } function changeImg(){ for(var i=0;i<size;i++){ pics[i].style.display = "none"; } pics[index].style.display = "block"; } function slideImg(){ var main = byId("main"); main.onmouseover = function(){ } main.onmouseout = function(){ timer = setInterval(function() { index++; if (index >= size) index = 0; //切换图片 changeImg(); }, 2000); } } slideImg();
祝学习愉快!
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>综合案例</title>
<link rel="stylesheet" type="text/css" href="综合案例.css">
<body>
<div class="main">
<div class="banner">
<a href="javascript:void();">
<div class="banner-slide slide1 slide-active"></div>
</a>
<a href="">
<div class="banner-slide slide2 "></div>
</a>
<a href="">
<div class="banner-slide slide3"></div>
</a>
</div>
<a class=" button prev" id="prev"></a>>
<a class=" button next" id="next"></a>
<div class="dots">
<span class="active"></span>
<span></span>
<span></span>
</div>
</div>
<script src="综合案例.js"> </script>
</body>
</html>
*{
margin: 0;
padding: 0;
}
body{
font-family: "微软雅黑";
color: #14191e;
}
.main{
width: 1200px;
height: 460px;
overflow: hidden;
margin: 30px auto;
position: relative;
}
.banner{
width: 1200px;
height: 460px;
overflow: hidden;
position: relative;
}
.banner-slide{
width: 1200px;
height: 460px;
background-repeat: no-repeat;
position: absolute;
display: none;
}
.slide1{
background-image:url(img\\bg1.jpg);
}
.slide2{
background-image:url(img\\bg2.jpg);
}
.slide3{
background-image:url(img\\bg3.jpg);
}
.slide-active{
display: block;
}
.button{
position: absolute;
width: 40px;
height: 80px;
left: 244px;
top: 50%;
margin-top: -40px;
background: url("img\\arrow.png") no-repeat center center;
/*background-color: gray;*/
}
.prev{
transform: rotate(180deg);
}
.next{
right: 0;
left: auto;
}
.button:hover{
background-color: #333;
opacity: 0.5;
}
.dots{
position: absolute;
bottom: 20px;
right: 0;
text-align: right;
margin-right: 30px;
}
.dots span{
width: 12px;
height: 12px;
border-radius: 50%;
line-height: 12px;
/*background-color:black;
opacity: 0.4;*/
background:rgba(7,17,27,0.4);
/*添加阴影*/
box-shadow: 0 0 0 1px rgba(255,255,255,0.8) inset;
display: inline-block;
margin-left: 10px;
cursor: pointer;/*光标形状*/
}
.active{
background: #fff;
box-shadow: 0 0 0 1px rgba(7,17,27,0.4) inset;
}
//封装getElementById()
function byId(id){
return typeof(id)==="String"?document.getElementById('id'):id;
}
var timer=null;
var pics=byId("banner").getElementsByTagName("div");
var dots=byId("dots").getElementsByTagName('span');
var prev=byId("prev");
var next=byId("next");
var len=pics.length;
var index=0;
function slideImg(){
//取出mian元素
var main=byId("main");
//鼠标放置在图片上
main.onmouseover=function(){
if(timer)
clearInterval(timer);
}
//鼠标从图片上移走时,调用onmouseout事件
main.onmouseout=function(){
timer= setInterval(function(){
index++;
if(index==len)
index=0;
//切换图片
changeImg();
},2000);
}
//打开页面自动执行图片轮播,调用onmouseout方法
main.onmouseout();
//遍历所有点击,且绑定点击事件,点击圆点切换图片
for(var d=0;d<len;d++)
{
//给所有span添加一个id的属性,值为d,作为当前span的索引
dots[d].id=d;
dots[d].onclick=function(){
idnex=this.id;
chingeImg();
}
}
//下一张
next.onclick=function(){
index++;
if (index>=len)
{
index=0;
}
changeImg();
}
//上一张
prev.onclick=function(){
idnex--;
if (index<0)
{
index=len-1;
}
changeImg();
}
}
//切换图片
function changeImg(){
//遍历banner里所有的div,将其隐藏
for(var i=0;i<len;i++)
{
pics[i].style.display="none";
dots[i].className=" ";
}
//根据索引找到当前div,将其显示出来
pics[index].style.display='block';
docs[index].className="active";
}
slideImg();
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星