我希望在点击时更改按钮的图像,然后在点击发布时更改。我目前有一个平面图像,我希望它更改为带有内阴影的图像,使其看起来像按下按钮,但在释放鼠标单击时更改回原始的平面图像。我试过onmouseup,但我认为我没有错误地使用它。我希望"item"
和"item2"
在点击时保留其图像,并且正常工作。但是我希望"item3"
在鼠标释放后更改,或者在点击后立即更改。
(编辑:我是JavaScript的新手,它不一定是onmouseup
解决方案。如果有人可以解释如何创建一个能够做到这一点的功能那么那就太棒了。 )
JS:
var onImgStp= "images/stop.png";
var onImgPnk= "images/pink.png";
var onImgMut= "images/mute.png";
var offImg= "images/green.png";
var offImgStp= "images/stop2.png";
HTML:
<img class="item" src="images/pink.png" onclick="manage(1); this.src = (this.src.endsWith(offImg)? onImgPnk : offImg);"/>
<img class="item" src="images/pink.png" onclick="manage(2); this.src = (this.src.endsWith(offImg)? onImgPnk : offImg);"/>
<img class="item" src="images/pink.png" onclick="manage(3); this.src = (this.src.endsWith(offImg)? onImgPnk : offImg);"/>
<img class="item" src="images/pink.png" onclick="manage(4); this.src = (this.src.endsWith(offImg)? onImgPnk : offImg);"/>
<img class="item" src="images/pink.png" onclick="manage(5); this.src = (this.src.endsWith(offImg)? onImgPnk : offImg);"/>
<img class="item" src="images/pink.png" onclick="manage(6); this.src = (this.src.endsWith(offImg)? onImgPnk : offImg);"/>
<img class="item" src="images/pink.png" onclick="manage(7); this.src = (this.src.endsWith(offImg)? onImgPnk : offImg);"/>
<img class="item" src="images/pink.png" onclick="manage(8); this.src = (this.src.endsWith(offImg)? onImgPnk : offImg);"/>
<img class="item2" src="images/mute.png" onclick="this.src = (this.src.endsWith(offImg)? onImgMut : offImg);"/>
<img class="item3" src="images/stop.png" onmouseup="this.src = (this.src.endsWith(offImgStp)? onImgStp : offImgStp);"/>
答案 0 :(得分:3)
.image-swap {
cursor: pointer;
}
.image-swap>img, .image-swap:active>img:first-child {
display: none;
}
.image-swap>img:first-child, .image-swap:active>img:last-child {
display: block;
}
&#13;
<div class="image-swap">
<img src="http://blog.hvidtfeldts.net/media/city7.png" />
<img src="http://blog.hvidtfeldts.net/media/city2.png" />
</div>
&#13;
.image-swap {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-o-user-select: none;
user-select: none;
cursor: pointer;
}
.image-swap>img {
pointer-events: none;
display: none;
}
.image-swap>img:first-child {
display: block;
}
.image-swap:active>img:first-child {
display: none;
}
.image-swap:active>img:last-child {
display: block;
}
&#13;
<div class="image-swap">
<img src="http://blog.hvidtfeldts.net/media/city7.png" />
<img src="http://blog.hvidtfeldts.net/media/city2.png" />
</div>
&#13;
.image-swap {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-o-user-select: none;
user-select: none;
cursor: pointer;
position: relative;
}
.image-swap>img {
pointer-events: none;
transition: all 0.6s;
position: absolute;
opacity: 0;
left: 0;
}
.image-swap>img:first-child {
position: static;
opacity: 1;
}
.image-swap:active>img:first-child {
opacity: 0;
}
.image-swap:active>img:last-child {
opacity: 1;
}
&#13;
<div class="image-swap">
<img src="http://blog.hvidtfeldts.net/media/city7.png" />
<img src="http://blog.hvidtfeldts.net/media/city2.png" />
</div>
&#13;
如果您愿意,可以为base-img
和active-img
因此,您可以将:first-child
替换为.base-img
,将:last-child
替换为.active-img
答案 1 :(得分:2)
看看这个非常简单的概念验证解决方案(基于您的问题和评论):
function mousedown() {
var el = document.getElementById("image01");
el.setAttribute("src", "http://blog.fantasy.co/wp-content/uploads/2013/02/feature_Net.jpg")
}
function resetImage() {
var el = document.getElementById("image01");
el.setAttribute("src", "https://camo.githubusercontent.com/b87a252140848659b80b0d2297e32dc62afee0cf/68747470733a2f2f646f63732e6d6963726f736f66742e636f6d2f656e2d75732f646f746e65742f61727469636c65732f696d616765732f6875622f6e6574636f72652e737667")
}
<img id="image01" src="https://camo.githubusercontent.com/b87a252140848659b80b0d2297e32dc62afee0cf/68747470733a2f2f646f63732e6d6963726f736f66742e636f6d2f656e2d75732f646f746e65742f61727469636c65732f696d616765732f6875622f6e6574636f72652e737667" alt="image" onmousedown="mousedown()" onmouseup="resetImage();" onmouseleave="resetImage();" />
通常情况下,我会建议使用CSS (如果你还需要不止一个,可能需要拼接图像)来实现这个效果。