我昨天用一个不同的查询发布了下面的代码,但现在我想知道如何使用DRY方法组合这两个函数,因为它们在本质上是相同的。
任何帮助都非常感谢。谢谢。
已编辑完整代码...
$(function(){
//Supersize Image
$.fn.supersized.options = {
startwidth: 1278,
startheight: 800,
vertical_center: 0,
slides : [{image : ""}]
};
$('#supersized').supersized();
//Image Gallery
var imgs = [
['images/test.jpg',
'Test Title',
'Test text',
'light'],
['images/test.jpg',
'Test Title',
'Test text',
'light'],
['images/test.jpg',
'Test Title',
'Test text',
'dark']
];
var cnt = imgs.length;
var lengthMinusOne = cnt - 1,
index = 0,
fadeSpeed = 1000;
preload_image_object = new Image();
var i = 0;
for (i = 0; i <= cnt; i++)
preload_image_object.src = imgs[i];
$("#txt h1").text(imgs[0][1]);
$("#txt #desc p").text(imgs[0][2]);
var ld = imgs[0][3];
if (ld == "dark") {
$("body").addClass("dark");
};
var firstImg = $('<img />');
$(firstImg).attr('src', imgs[0][0]);
$('#supersized').append(firstImg);
$(firstImg).hide().fadeIn(fadeSpeed);
$("#prev-photo").bind('click', prev);
function prev() {
index--;
$('#prev-photo,#next-photo').unbind();
if (index < 0) {
index = lengthMinusOne;
};
var ld = imgs[index][3];
if (ld == "dark") {
$("body").addClass("dark");
} else {
$("body").removeClass("dark");
};
oldImg = $('#supersized img').addClass('old');
$("#txt h1").text(imgs[index][1]).hide().fadeIn();
$("#txt #desc p").text(imgs[index][2]).hide().fadeIn();
var img = new Image();
$(img).load(function () {}).error(function () {}).attr('src', imgs[index][0]);
$('#supersized').append(img);
$('#supersized img').css('left', '0');
$(img).hide().fadeIn(fadeSpeed, function () {
oldImg.remove();
$('#prev-photo').bind('click', prev);
$('#next-photo').bind('click', next);
});
};
$("#next-photo").bind('click', next);
function next() {
index++;
$('#next-photo,#prev-photo').unbind();
if (index > lengthMinusOne) {
index = 0
};
var ld = imgs[index][3];
if (ld == "dark") {
$("body").addClass("dark");
} else {
$("body").removeClass("dark");
};
oldImg = $('#supersized img').addClass('old');
$("#txt h1").text(imgs[index][1]).hide().fadeIn();
$("#txt #desc p").text(imgs[index][2]).hide().fadeIn();
var img = new Image();
$(img).load(function () {}).error(function () {}).attr('src', imgs[index][0]);
$('#supersized').append(img);
$('#supersized img').css('left', '0');
$(img).hide().fadeIn(1300, function () {
oldImg.remove();
$('#next-photo').bind('click', next);
$('#prev-photo').bind('click', prev);
});
};
$("#gallery-buttons").css("bottom", "-220px");
$("#project-name").click(function () {
$(this).fadeOut(500, function () {
$("#gallery-buttons").animate({
bottom: '0'
}, 800);
});
});
$("#hide-caption").click(function () {
$("#gallery-buttons").animate({
bottom: '-220px'
}, 800, function () {
$("#project-name").fadeIn(500);
});
});
});以下是所请求的HTML ...
<div id="buttons-wrap">
<div id="prev-photo">prev photo</div>
<div id="next-photo">next photo</div>
</div>
<div id="supersized"></div>发布于 2011-04-01 16:49:34
如果您发布的代码有效,那么这也应该是...
$("#prev-photo").bind ('click', {bIsNext: false}, ImageFoo);
$("#next-photo").bind ('click', {bIsNext: true}, ImageFoo);
function ImageFoo (zEvent) {
if (zEvent.data.bIsNext) index++;
else index--;
if (index < 0) index = lengthMinusOne;
if (index > lengthMinusOne) index = 0;
$('#prev-photo,#next-photo').unbind();
var ld = imgs[index][3];
if (ld == "dark") {
$("body").addClass("dark");
} else {
$("body").removeClass("dark");
};
oldImg = $('#supersized img').addClass('old');
$("#txt h1").text(imgs[index][1]).hide().fadeIn();
$("#txt #desc p").text(imgs[index][2]).hide().fadeIn();
var img = new Image();
$(img).load(function () {}).error(function () {}).attr('src', imgs[index][0]);
$('#supersized').append(img);
$('#supersized img').css('left', '0');
$(img).hide().fadeIn(fadeSpeed, function () {
oldImg.remove();
$('#next-photo').bind ('click', {bIsNext: true}, ImageFoo);
$('#prev-photo').bind ('click', {bIsNext: false}, ImageFoo);
});
};发布于 2011-04-01 16:31:22
var changePicture = function(direction) {
if (direction>0) {
// Code to initialise eveything specific to next picture
} else {
// Code to initialise eveything specific to previous picture
}
// Code to execute in both cases using everything initialised above
};
var prev = function() {
changePicture(-1);
};
var next = function() {
changePicture(1);
};
$("#prev-photo").bind('click', prev);
$("#next-photo").bind('click', next);发布于 2011-04-01 16:31:36
为什么不给函数添加参数,这只会改变它的行为呢?这就是创建函数参数的目的。
(如果你需要,我会马上重写你的函数)
所以,它是这样的:
bindButtons();
function change(direction) {
if (direction == 'prev') { index = (index -1) < 0 ? lengthMinusOne : index -1, speed = fadeSpeed; }
else { index = (index +1) > lengthMinusOne ? 0 : index +1, speed = 1300; }
$('#prev-photo,#next-photo').unbind();
var ld = imgs[index][3], body = $("body");
if (ld == "dark") { body.addClass("dark"); }
else { body.removeClass("dark"); };
var oldImg = $('#supersized img').addClass('old');
$("#txt h1").text(imgs[index][1]).hide().fadeIn();
$("#txt #desc p").text(imgs[index][2]).hide().fadeIn();
var img = new Image();
$(img).load(function () {}).error(function () {}).attr('src', imgs[index][0]);
$('#supersized').append(img);
$('#supersized img').css('left', '0');
$(img).hide().fadeIn(speed, function () {
oldImg.remove();
bindButtons();
});
};
function bindButtons() {
$("#prev-photo").bind('click', change('prev'));
$("#next-photo").bind('click', change('next'));
}你的代码非常不干净,我认为,如果提供HTML,它甚至可以被优化得更多,因为这些东西是用来做什么的。
https://stackoverflow.com/questions/5510820
复制相似问题