有什么方法可以缩短我的相同功能的代码吗?
我必须在我的网站上运行许多相同的功能
<section class="leilig-right" id="leilig-right-box1">
<section class="glwrap x1">
<ul class="gallery clearfix">
<div id="navz">
<span id="prev" class="prev" style="cursor:pointer;"></span>
<span id="next" class="next" style="cursor:pointer;"></span>
</div>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
</ul>
</section>
</section>
,并且...
<section class="leilig-right" id="leilig-right-box1">
<section class="glwrap x2">
<ul class="gallery clearfix">
<div id="navz">
<span id="prev" class="prev" style="cursor:pointer;"></span>
<span id="next" class="next" style="cursor:pointer;"></span>
</div>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
</ul>
</section>
</section>
以及我的相同功能,并且
function scrollToPositionx1(element) {
if (element !== undefined) {
$(".x1").scrollTo(element, 800, {
margin: true
});
}
}
$(function() {
//Create an Array of posts
var posts = $('.x1 img');
var position = 0; //Start Position
var next = $('.x1 #next');
var prev = $('.x1 #prev').hide();
//Better performance to use Id selectors than class selectors
next.click(function(evt) {
//Scroll to next position
prev.show();
scrollToPositionx1(posts[position += 1]);
if (position === posts.length - 6) {
next.hide();
}
});
prev.click(function(evt) {
//Scroll to prev position
next.show();
scrollToPositionx1(posts[position -= 1]);
if (position === 0) {
prev.hide();
}
});
});
并且...
function scrollToPositionx2(element) {
if (element !== undefined) {
$(".x2").scrollTo(element, 800, {
margin: true
});
}
}
$(function() {
//Create an Array of posts
var posts = $('.x2 img');
var position = 0; //Start Position
var next = $('.x2 #next');
var prev = $('.x2 #prev').hide();
//Better performance to use Id selectors than class selectors
next.click(function(evt) {
//Scroll to next position
prev.show();
scrollToPositionx2(posts[position += 1]);
if (position === posts.length - 6) {
next.hide();
}
});
prev.click(function(evt) {
//Scroll to prev position
next.show();
scrollToPositionx2(posts[position -= 1]);
if (position === 0) {
prev.hide();
}
});
});
它仅 X1,X2,...等不同 有
什么方法可以缩短我的相同功能的代码吗?
I have to many same functions running on my site
<section class="leilig-right" id="leilig-right-box1">
<section class="glwrap x1">
<ul class="gallery clearfix">
<div id="navz">
<span id="prev" class="prev" style="cursor:pointer;"></span>
<span id="next" class="next" style="cursor:pointer;"></span>
</div>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
</ul>
</section>
</section>
and and and ...
<section class="leilig-right" id="leilig-right-box1">
<section class="glwrap x2">
<ul class="gallery clearfix">
<div id="navz">
<span id="prev" class="prev" style="cursor:pointer;"></span>
<span id="next" class="next" style="cursor:pointer;"></span>
</div>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
<li class="g2">
<a href="#">
<img src="http://www.supershareware.com/images/icons/Future_City_3D_Screensaver-31411.gif">
</a>
</li>
</ul>
</section>
</section>
and my same function too
function scrollToPositionx1(element) {
if (element !== undefined) {
$(".x1").scrollTo(element, 800, {
margin: true
});
}
}
$(function() {
//Create an Array of posts
var posts = $('.x1 img');
var position = 0; //Start Position
var next = $('.x1 #next');
var prev = $('.x1 #prev').hide();
//Better performance to use Id selectors than class selectors
next.click(function(evt) {
//Scroll to next position
prev.show();
scrollToPositionx1(posts[position += 1]);
if (position === posts.length - 6) {
next.hide();
}
});
prev.click(function(evt) {
//Scroll to prev position
next.show();
scrollToPositionx1(posts[position -= 1]);
if (position === 0) {
prev.hide();
}
});
});
and and and ...
function scrollToPositionx2(element) {
if (element !== undefined) {
$(".x2").scrollTo(element, 800, {
margin: true
});
}
}
$(function() {
//Create an Array of posts
var posts = $('.x2 img');
var position = 0; //Start Position
var next = $('.x2 #next');
var prev = $('.x2 #prev').hide();
//Better performance to use Id selectors than class selectors
next.click(function(evt) {
//Scroll to next position
prev.show();
scrollToPositionx2(posts[position += 1]);
if (position === posts.length - 6) {
next.hide();
}
});
prev.click(function(evt) {
//Scroll to prev position
next.show();
scrollToPositionx2(posts[position -= 1]);
if (position === 0) {
prev.hide();
}
});
});
it different only X1, X2, ... and more
Any way to short code my same functions ?
Live demo : http://jsfiddle.net/G3ERL/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你应该使用 http://jquery.malsup.com/cycle/nest2.html
并只需使用 http://jquery.malsup.com/cycle/ 并且它没有错误并且更多的人为错误修复做出了贡献:)
差点忘了,它总是可重用的哈哈:)与你的函数不同:)
I think you should just use http://jquery.malsup.com/cycle/nest2.html
and just use http://jquery.malsup.com/cycle/ and its bug free and many more people contribute to bug fixes :)
nearly forgot, its always reusable lol :) unlike ur functions :)