我有几张图片以及它们的悬停图片。使用jQuery,我想在发生onmousemove/onmouseout事件时显示/隐藏悬停图片。所有我的图像名称都遵循相同的模式,如下:
原始图片:
Image.gif
悬停图片:
Imageover.gif
我想在onmouseover和onmouseout事件中分别插入和删除图像源中的“over”部分。
我该如何使用jQuery实现?
我有几张图片以及它们的悬停图片。使用jQuery,我想在发生onmousemove/onmouseout事件时显示/隐藏悬停图片。所有我的图像名称都遵循相同的模式,如下:
原始图片:
Image.gif
悬停图片:
Imageover.gif
我想在onmouseover和onmouseout事件中分别插入和删除图像源中的“over”部分。
我该如何使用jQuery实现?
为了设置“on ready”:
$(function() {
$("img")
.mouseover(function() {
var src = $(this).attr("src").match(/[^\.]+/) + "over.gif";
$(this).attr("src", src);
})
.mouseout(function() {
var src = $(this).attr("src").replace("over.gif", ".gif");
$(this).attr("src", src);
});
});
对于那些使用URL图片来源的人:
$(function() {
$("img")
.mouseover(function() {
var src = $(this).attr("src");
var regex = /_normal.svg/gi;
src = this.src.replace(regex,'_rollover.svg');
$(this).attr("src", src);
})
.mouseout(function() {
var src = $(this).attr("src");
var regex = /_rollover.svg/gi;
src = this.src.replace(regex,'_normal.svg');
$(this).attr("src", src);
});
});
我知道你在问如何使用jQuery,但你可以通过CSS在浏览器禁用JavaScript时达到相同的效果:
#element {
width: 100px; /* width of image */
height: 200px; /* height of image */
background-image: url(/path/to/image.jpg);
}
#element:hover {
background-image: url(/path/to/other_image.jpg);
}
这里有一个更详细的描述。
然而更好的方法是使用精灵图:simple-css-image-rollover
<img data-other-src="big-zebra.jpg" src="small-cat.jpg">
<img data-other-src="huge-elephant.jpg" src="white-mouse.jpg">
<img data-other-src="friendly-bear.jpg" src="penguin.jpg">
JavaScript
$('img').bind('mouseenter mouseleave', function() {
$(this).attr({
src: $(this).attr('data-other-src')
, 'data-other-src': $(this).attr('src')
})
});
/* Teaser image swap function */
$('img.swap').hover(function () {
this.src = '/images/signup_big_hover.png';
}, function () {
this.src = '/images/signup_big.png';
});
<img src="img1.jpg" onmouseover="swap('img2.jpg')" onmouseout="swap('img1.jpg')" />
function swap(newImg){
this.src = newImg;
}
<img src="img1.jpg" id="ref1" />
<img src="img3.jpg" id="ref2" />
<img src="img5.jpg" id="ref3" />
JavaScript / jQuery
// Declare Arrays
imgList = new Array();
imgList["ref1"] = new Array();
imgList["ref2"] = new Array();
imgList["ref3"] = new Array();
//Set values for each mouse state
imgList["ref1"]["out"] = "img1.jpg";
imgList["ref1"]["over"] = "img2.jpg";
imgList["ref2"]["out"] = "img3.jpg";
imgList["ref2"]["over"] = "img4.jpg";
imgList["ref3"]["out"] = "img5.jpg";
imgList["ref3"]["over"] = "img6.jpg";
//Add the swapping functions
$("img").mouseover(function(){
$(this).attr("src", imgList[ $(this).attr("id") ]["over"]);
}
$("img").mouseout(function(){
$(this).attr("src", imgList[ $(this).attr("id") ]["out"]);
}
$('img.over').each(function(){
var t=$(this);
var src1= t.attr('src'); // initial src
var newSrc = src1.substring(0, src1.lastIndexOf('.'));; // let's get file name without extension
t.hover(function(){
$(this).attr('src', newSrc+ '-over.' + /[^.]+$/.exec(src1)); //last part is for extension
}, function(){
$(this).attr('src', newSrc + '.' + /[^.]+$/.exec(src1)); //removing '-over' from the name
});
});
您可能需要更改首行图像的类。如果需要更多的图片类(或不同的路径),可以使用
$('img.over, #container img, img.anotherOver').each(function(){
我希望有一个像超级一行代码那样的东西:
$("img.screenshot").attr("src", $(this).replace("foo", "bar"));
如果您正在寻找的解决方案是用于动画按钮,那么您可以通过精灵图和CSS的结合来提高性能。 精灵图是一个包含站点上所有图像(标题、徽标、按钮以及所有装饰)的巨大图像。 每个图像都使用一个HTTP请求,HTTP请求越多,加载时间就越长。
.buttonClass {
width: 25px;
height: 25px;
background: url(Sprite.gif) -40px -500px;
}
.buttonClass:hover {
width: 25px;
height: 25px;
background: url(Sprite.gif) -40px -525px;
}
0px 0px
坐标将是您的精灵左上角。
但如果您正在使用Ajax或类似工具开发相册,那么JavaScript(或任何框架)是最好的选择。
玩得开心!
$('img').mouseover(function(){
var newSrc = $(this).attr("src").replace("image.gif", "imageover.gif");
$(this).attr("src", newSrc);
});
$('img').mouseout(function(){
var newSrc = $(this).attr("src").replace("imageover.gif", "image.gif");
$(this).attr("src", newSrc);
});
<script type="text/javascript">
$(document).ready(function() {
$("img", this).hover(swapImageIn, swapImageOut);
function swapImageIn(e) {
this.src = this.src.replace("_off", "_on");
}
function swapImageOut (e) {
this.src = this.src.replace("_on", "_off");
}
});
</script>