Jquery优化代码
我昨天发布了带有不同查询的下面的代码,但我现在想知道如何使用 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>
I posted the code below with a diffrent query yesterday but I would like to now know how to combine the two functions as they are bascially the same, using the DRY method.
Any help much appreciated. Thanks.
Edited full code...
$(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);
});
});
});
Here is HTML as requested...
<div id="buttons-wrap">
<div id="prev-photo">prev photo</div>
<div id="next-photo">next photo</div>
</div>
<div id="supersized"></div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您发布的代码有效,那么这也应该......
If your posted code worked, then this should too...
为什么不将参数添加到函数中,这只会改变它的行为?这就是函数参数的用途。
(如果你需要的话,我会在一分钟内重写你的函数)
所以,这里是:
你的代码非常不干净,我认为,如果提供 HTML,它可以进一步优化,因为这个东西是用来做什么的。
Why dont you add the parameter to function, that just changes its behaviour? That is what function parameters are made for.
(I will rewrite your function in a minute, if you need)
So, here it is:
Your code is very unclean, I think, it can be optimised eaven more, if HTML would be supplied, for what this stuff is ment for.