题
我有一个具有与多个缩略图上有一个大的图像的页。当鼠标移到缩略图的主图像变化的图像你刚刚推出你的鼠标。问题是更多缩略图我有,更多的重复代码,我有。我怎么能减少呢?
jQuery的代码如下所示。
<script type="text/javascript">
$('#thumb1')
.mouseover(function(){
$('#big_img').fadeOut('slow', function(){
$('#big_img').attr('src', '0001.jpg');
$('#big_img').fadeIn('slow');
});
});
$('#thumb2')
.mouseover(function(){
$('#big_img').fadeOut('slow', function(){
$('#big_img').attr('src', 'p_0002.jpg');
$('#big_img').fadeIn('slow');
});
});
$('#thumb3')
.mouseover(function(){
$('#big_img').fadeOut('slow', function(){
$('#big_img').attr('src', '_img/p_0003.jpg');
$('#big_img').fadeIn('slow');
});
});
$('#thumb4')
.mouseover(function(){
$('#big_img').fadeOut('slow', function(){
$('#big_img').attr('src', '0004.jpg');
$('#big_img').fadeIn('slow');
});
});
</script>
#big_img
=全尺寸图像
#thumb1
,#thumb2
,#thumb3
,#thumb4
=缩略图
有关的页中的主代码是PHP是否有帮助。
解决方案
首先,你应该修改你的代码,以便每个缩略图有一个容易找到的“类”。
假设你有
<img id="thumb1" scr=""/>
<img id="thumb2" scr=""/>
..
您的HTML应该像
<img id="thumb1" class='thumb' scr=""/>
<img id="thumb2" class='thumb' scr=""/>
..
其次,你应该确保你有你的缩略图和他们的大图像配合件之间的可识别模式。在你的代码中,我们有
0001.jpg
p_0002.jpg
_img/p_0003.jpg
0004.jpg
什么是你组织你的文件?
让我们想象一下现在的缩略图具有相同src作为错误图像
在jQuery代码将被收缩到:
$('.thumb').mouseover(function(){
var thumb_src = $(this).attr('src');
// ==> add code here to transform thumb_src into the big_img src
$('#big_img').fadeOut('slow', function(){
$('#big_img').attr('src', thumb_src);
$('#big_img').fadeIn('slow');
});
});
此代码应工作,并用于算法只是等待该变换拇指的src入大图像的src
我希望这会帮助你 杰罗姆瓦格纳
其他提示
$(this)
是你的朋友。您还需要制定某种形式的命名,你可以用它来搭配你的文件名和身份证的的。我通常做的是用PHP生成HTML,但摆在那处理文件名的特殊属性:
// PHP GeneratedContent
<?php
while(/* some range */) {
$i = 1;
$output .= '<element class="thumb" rel="p_' . $i . '.jpg">';
$i++;
}
echo $output;
?>
然后我会去了解下一个步骤:
$('.thumb').mouseover( function() {
var link = $(this).attr('rel');
/* Now that you have the link, just put it into whatever function you need to */
});
Fehays方法肯定能行,但这样一来,你不会有不必要吨ID的,而你将不必进行不必要的参数传递。它会自动传播到每一个实例在与类thumb
的DOM。
您可以只写一个函数,我认为。
applyImageFade('#thumb1','0001.jpg');
applyImageFade('#thumb2','p_0002.jpg');
//etc...
function applyImageFade(thumbId, image) {
$(thumbId).mouseover(function(){
$('#big_img').fadeOut('slow', function(){
$('#big_img').attr('src', image);
$('#big_img').fadeIn('slow');
});
});
}
我觉得你可以做的事情干净将扩大jQuery的,包括你想要的功能:
//random images pulled off of Google Image Search
src1 = "http://www.o3mobi.com/jquery.jpg";
src2 = "http://www.bioneural.net/wp-content/uploads/2008/02/jquery.jpg";
$.fn.fadeToSrc = function(src, speedOut, speedIn, callback) {
return this.fadeOut(speedOut, function() {
$(this).attr('src', src).fadeIn(speedIn, callback);
});
};
$("#image").click(function () {
$(this).fadeToSrc(src2, 1000, 4000);
});
您可以在这里测试一下吧! http://jsfiddle.net/jPYyZ/
==更新=======
如果你想要做一个鼠标悬停缩略图/预览系统,这是所有需要:
//HTML
<img class="thumbnail" src="http://www.o3mobi.com/jquery.jpg">
<img class="thumbnail" src="http://www.bioneural.net/wp-content/uploads/2008/02/jquery.jpg">
<img id="fullsized">
//CSS
.thumbnail {
width: 5em;
height: 5em;
}
#fullsized {
width: 10em;
height: 10em;
border: 2px solid red;
}
//JAVASCRIPT
$.fn.fadeToSrc = function(src, speedOut, speedIn, callback) {
return this.fadeOut(speedOut, function() {
$(this).attr('src', src).fadeIn(speedIn, callback);
});
};
$(".thumbnail").mouseover(function () {
var newsrc = $(this).attr("src");
$("#fullsized").fadeToSrc(newsrc, 1000, 1000);
});
您可以围绕测试/鼓捣这个位置: http://jsfiddle.net/AhwH7/一>
不隶属于 StackOverflow