带有onclick切换的Javascript图像滚动
Javascript Image roll over with onclick toggle
我目前有代码可以滚动图像链接,如下所示:
<a id="show" href="#"><img src="images/show-me.png" border=0 onmouseover="this.src='images/show-me_over.png';" onmouseout="this.src='images/show-me.png'"></a>
单击此按钮会显示一个div
jQuery(document).ready(function(){
jQuery("#show").toggle(
function(){
jQuery('#home-hidden-extra').animate({'height': '480px'} ,1000);},
function(){
jQuery('#home-hidden-extra').animate({'height': '0px'} ,1000);}
);
});
我想做的是,当div被隐藏时,使用show_me.png和show_me-over.png,当div显示时,使用hide_me.png和hide_me-over.png。
实现这一点的最简单方法是什么?
再次感谢!
您应该将静态悬停样式放到CSS中。定义
.linkShow {
background-image: url("images/show_me.png");
}
.linkShow:hover {
background-image: url("images/show_me_hover.png");
}
.linkHide {
background-image: url("images/hide_me.png");
}
.linkHide:hover {
background-image: url("images/hide_me_hover.png");
}
然后使用jquery将这些类添加到链接中。
$("#show").removeClass().addClass("linkShow");
这应该可以工作:
HTML
<a id="show" href="#">
<img id="the-image" src="images/show-me.png" />
</a>
JS
$(document).ready(function(){
setupHover(); /* Defined below */
$('#show').toggle(function(){
$('#home-hidden-extra').animate({'height': '480px'} ,1000);
setupHover();
},function(){
$('#home-hidden-extra').animate({'height': '0px'} ,1000);
setupHover();
});
});
function setupHover(){
/* Unbind existing hover handlers */
$('#show').unbind('mouseenter mouseleave');
/* Use the correct image filenames depending on the situation */
if($('#home-hidden-extra').height() > 0){
images = ['show-me.png','show-me_over.png'];
}
else {
images = ['hide_me.png','hide_me-over.png'];
}
$('#show').hover(function(){
$('#the-image').attr('src','images/' + images[1]);
},function(){
$('#the-image').attr('src','images/' + images[0]);
});
}
相关文章:
- 如何在JavaScript图像滑块内居中对齐图像
- Javascript图像链接
- Javascript:图像与单选按钮的值不匹配
- 具有相同大小图像的Javascript图像面板
- Javascript图像编辑插件
- Javascript图像刷新
- Javascript图像数组预加载速度和从内存中删除
- JavaScript图像横幅计时器HTML
- Javascript图像数组
- HTML5/JavaScript图像点击事件
- Javascript图像宽度和高度未定义值
- 请解释Javascript图像旋转代码
- Javascript:图像按钮
- 打开新窗口进行打印时不显示 JavaScript 图像
- JavaScript 图像加载器不起作用
- 如何使用Javascript图像裁剪器“Croppie”保存
- 随着模块数量的增加,Javascript图像正在重复
- JavaScript图像点击错误
- Javascript图像幻灯片;不起作用
- 简单的图像滑块Javascript图像路径在IE 6中不起作用