Javascript 使用jquery自动化滑块?
我有一个滑块,使用以下功能,点击按钮即可滑动图像Javascript 使用jquery自动化滑块?,javascript,jquery,Javascript,Jquery,我有一个滑块,使用以下功能,点击按钮即可滑动图像 $(document).ready(function (){ $('#button a').click(function(){ var integer = $(this).attr('rel'); $('#myslide .cover').animate({left:-720*(parseInt(integer)-1)}) $('#button a').each(function(){ $(this).remov
$(document).ready(function (){
$('#button a').click(function(){
var integer = $(this).attr('rel');
$('#myslide .cover').animate({left:-720*(parseInt(integer)-1)})
$('#button a').each(function(){
$(this).removeClass('active');
if($(this).hasClass('button'+integer)){
$(this).addClass('active')}
});
});
});
是否可以使用jquery自动执行此操作
刚发现这个oly不确定我将如何实施它
setInterval(function() {
// Do something every 2 seconds
}, 2000);
首先,将执行动画的函数设置为一级函数(即,为其命名并将其移出click()): 然后将单击处理程序与以下内容关联:
$('#button a').click(animate);
然后,使用setInterval()或setTimeout()在指定的超时后执行该函数:
setTimeout(animate, 3000);
首先,将执行动画的函数设置为一级函数(即,为其命名并将其移出click()): 然后将单击处理程序与以下内容关联:
$('#button a').click(animate);
然后,使用setInterval()或setTimeout()在指定的超时后执行该函数:
setTimeout(animate, 3000);
要在单击按钮后设置图像动画,可以使用setInterval。我前面的示例使用了setTimeout,即使在您提到setInterval之后。哎呀。这应该干净得多 评论应该描述正在发生的事情 请注意,按下按钮时会调用setInterval。每次按下按钮,它都会启动另一个实例。对此的修复可以归结为您想要使用的编码逻辑。如果必须将其设置为可以多次调用,请尝试使用clearInterval()
$(function() {
//Click our link
$("a").click(function() {
animateOurImage();
setInterval(function() { animateOurImage(); }, 3000);
//Be sure you account for the animation's duration in setInterval.
});
});
function animateOurImage() {
// We need the .each to access the image with $(this)
$('.image').animate({
left: "-=120" //relative to current position
}, 1000); //Duration
}
下面是一个提琴示例:要在单击按钮后设置图像动画,可以使用setInterval。我前面的示例使用了setTimeout,即使在您提到setInterval之后。哎呀。这应该干净得多 评论应该描述正在发生的事情 请注意,按下按钮时会调用setInterval。每次按下按钮,它都会启动另一个实例。对此的修复可以归结为您想要使用的编码逻辑。如果必须将其设置为可以多次调用,请尝试使用clearInterval()
$(function() {
//Click our link
$("a").click(function() {
animateOurImage();
setInterval(function() { animateOurImage(); }, 3000);
//Be sure you account for the animation's duration in setInterval.
});
});
function animateOurImage() {
// We need the .each to access the image with $(this)
$('.image').animate({
left: "-=120" //relative to current position
}, 1000); //Duration
}
这里有一个提琴示例:你说的自动是什么意思?那么在3秒钟后,它会自动滑动到下一张幻灯片。你到底想在这里自动执行什么操作?你说的自动是什么意思?那么在3秒钟后,它会自动滑动到下一张幻灯片。你到底想在这里自动执行什么操作?请注意,如果有多个.cover图像动画,这将启动许多计时器。您可以在.animate()范围之外设置一个变量,然后立即清除超时(variable),后跟variable=setTimeout()。这将使一次只有一个超时处于活动状态。或者,只需在函数外部调用setTimeout。这似乎并不是出于某种原因模拟按钮单击尝试我的编辑,请记住,将动画完全移动到一个新函数可能更实际,只需在单击事件中调用动画和setTimeout。clearTimeout()setTimeout()可能不是最干净的方式。请注意,如果多个.cover图像具有动画效果,则会启动许多计时器。您可以在.animate()范围之外设置一个变量,然后立即清除超时(variable),后跟variable=setTimeout()。这将使一次只有一个超时处于活动状态。或者,只需在函数外部调用setTimeout。这似乎并不是出于某种原因模拟按钮单击尝试我的编辑,请记住,将动画完全移动到一个新函数可能更实际,只需在单击事件中调用动画和setTimeout。clearTimeout()setTimeout()可能不是最干净的做事方式。