鼠标移到图片上,图片放大 怎么处理
我网上找的代码,可是这个有1张图片的时候没问题,但是有2张图片,第一张图片可以有放大效果,第二张就没有。到底是哪里有问题呢,帮忙看看,急等,高分求助<!DOCTYPEht...
我网上找的代码,可是这个有1张图片的时候没问题,但是有2张图片,第一张图片可以有放大效果,第二张就没有。到底是哪里有问题呢,帮忙看看,急等,高分求助
<!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">
<title>一张图片实现鼠标滑过缩略图显示大图片JS代码_懒人建站</title>
<head>
<style>
#demo{overflow:hidden;width:150px;text-align:center;padding:0px;}
#demo img{border:none;width:231px;height:250px;border:0px solid #f4f4f4}
#enlarge_images{position:absolute;display:none;z-index:2;border:5px solid #f4f4f4}
</style>
</head>
<body>
<div id="demo" style="overflow:hidden;width:231px;text-align:center;padding:0px"><img src="images/1.jpg" width="122" height="121" /></div>
<div id="enlarge_images"></div>
<div id="demo" style="overflow:hidden;width:231px;text-align:center;padding:0px"><img src="images/2.jpg" width="122" height="121" /></div>
<div id="enlarge_images"></div>
<script>
var demo = document.getElementById("demo");
var gg = demo.getElementsByTagName("img");
var ei = document.getElementById("enlarge_images");
for(i=0; i<gg.length; i++){
var ts = gg[i];
ts.onmousemove = function(event){
event = event || window.event;
ei.style.display = "block";
ei.innerHTML = '<img src="' + this.src + '" />';
ei.style.top = document.body.scrollTop + event.clientY + 10 + "px";
ei.style.left = document.body.scrollLeft + event.clientX + 10 + "px";
}
ts.onmouseout = function(){
ei.innerHTML = "";
ei.style.display = "none";
}
ts.onclick = function(){
window.open( this.src );
}
}
</script>
</body>
</html> 展开
<!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">
<title>一张图片实现鼠标滑过缩略图显示大图片JS代码_懒人建站</title>
<head>
<style>
#demo{overflow:hidden;width:150px;text-align:center;padding:0px;}
#demo img{border:none;width:231px;height:250px;border:0px solid #f4f4f4}
#enlarge_images{position:absolute;display:none;z-index:2;border:5px solid #f4f4f4}
</style>
</head>
<body>
<div id="demo" style="overflow:hidden;width:231px;text-align:center;padding:0px"><img src="images/1.jpg" width="122" height="121" /></div>
<div id="enlarge_images"></div>
<div id="demo" style="overflow:hidden;width:231px;text-align:center;padding:0px"><img src="images/2.jpg" width="122" height="121" /></div>
<div id="enlarge_images"></div>
<script>
var demo = document.getElementById("demo");
var gg = demo.getElementsByTagName("img");
var ei = document.getElementById("enlarge_images");
for(i=0; i<gg.length; i++){
var ts = gg[i];
ts.onmousemove = function(event){
event = event || window.event;
ei.style.display = "block";
ei.innerHTML = '<img src="' + this.src + '" />';
ei.style.top = document.body.scrollTop + event.clientY + 10 + "px";
ei.style.left = document.body.scrollLeft + event.clientX + 10 + "px";
}
ts.onmouseout = function(){
ei.innerHTML = "";
ei.style.display = "none";
}
ts.onclick = function(){
window.open( this.src );
}
}
</script>
</body>
</html> 展开
2个回答
展开全部
问题很简单,一看那个图片2就是你自己加上去的,删掉
<div id="demo" style="overflow:hidden;width:231px;text-align:center;padding:0px"><img src="images/2.jpg" width="122" height="121" /></div>
<div id="enlarge_images"></div>
把<img src="images/2.jpg" width="122" height="121">放到<img src="images/1.jpg" width="122" height="121">后面,再有图片三也是如此加。正解如下:
……
<body>
<div id="demo" style="overflow:hidden;width:231px;text-align:center;padding:0px">
<img src="images/1.jpg" width="122" height="121">
<img src="images/2.jpg" width="122" height="121">
<img src="images/3.jpg" width="122" height="121">
……
<img src="images/1000.jpg" width="122" height="121">
</div>
<div id="enlarge_images"></div>
……
<div id="demo" style="overflow:hidden;width:231px;text-align:center;padding:0px"><img src="images/2.jpg" width="122" height="121" /></div>
<div id="enlarge_images"></div>
把<img src="images/2.jpg" width="122" height="121">放到<img src="images/1.jpg" width="122" height="121">后面,再有图片三也是如此加。正解如下:
……
<body>
<div id="demo" style="overflow:hidden;width:231px;text-align:center;padding:0px">
<img src="images/1.jpg" width="122" height="121">
<img src="images/2.jpg" width="122" height="121">
<img src="images/3.jpg" width="122" height="121">
……
<img src="images/1000.jpg" width="122" height="121">
</div>
<div id="enlarge_images"></div>
……
追问
你这样就是所有的图片都放大了,我这不是连续的图片,可能第一张放大,第二张不放大
第三张又要放大 ,这怎么处理?
追答
郁闷,好不容易打了一大堆代码,窗口开的时间长了点,一点提交回答IE非法操作就全没了,百度知道有待改进啊!
还要重新打字……
(以下内容粘贴自记事本)。
如需指定某些缩略小图不显示原始大图,就需要在这些缩略小图中加个标记(id="hide",叫什么都行,注意前后要一致),在显示原始大图时进行判断,有标记的不显示,无标记的正常显示。
例如图片1和图片3不显示,代码如下:
……
……
var demo = document.getElementById("demo");
var gg = demo.getElementsByTagName("img");
var ei = document.getElementById("enlarge_images");
for(i=0; i';
ei.style.top = document.body.scrollTop + event.clientY + 10 + "px";
ei.style.left = document.body.scrollLeft + event.clientX + 10 + "px";
}
}//标记判断结束
ts.onmouseout = function(){
ei.innerHTML = "";
ei.style.display = "none";
}
ts.onclick = function(){
window.open( this.src );
}
}
需要注意的是,图片不要放到的外部,这个DIV在当前页面里是唯一的,一定要放到外面的话就需要再起个别的id名了,那么和都要作相应的改动。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询