How to make this slider auto play?(如何使此滑块自动播放?)
问题描述
我试图让这个滑块在 Javascript (jquery) 中自动播放,但我似乎无法让它工作,有谁知道我可以如何实现这一点?每次我尝试某些东西时,它似乎都会破坏滑块按钮.
I am trying to get this slider to auto play in Javascript (jquery) but I can't seem to get it to work, Does anyone know how I can achieve this? Every time I tried something it seemed to break the slider buttons.
jsfiddle 版本:http://jsfiddle.net/EsdB7/5/
jsfiddle version: http://jsfiddle.net/EsdB7/5/
这是当前代码:
jQuery(document).ready(function ($) {
var slideCount = $('#slider ul li').length;
var slideWidth = $('#slider ul li').width();
var slideHeight = $('#slider ul li').height();
var sliderUlWidth = slideCount * slideWidth;
$('#slider').css({ width: slideWidth, height: slideHeight });
$('#slider ul').css({ width: sliderUlWidth, marginLeft: - slideWidth });
$('#slider ul li:last-child').prependTo('#slider ul');
function moveLeft() {
$('#slider ul').animate({
left: + slideWidth
}, 200, function () {
$('#slider ul li:last-child').prependTo('#slider ul');
$('#slider ul').css('left', '');
});
};
function moveRight() {
$('#slider ul').animate({
left: - slideWidth
}, 200, function () {
$('#slider ul li:first-child').appendTo('#slider ul');
$('#slider ul').css('left', '');
});
};
$('a.control_prev').click(function () {
moveLeft();
});
$('a.control_next').click(function () {
moveRight();
});
});
推荐答案
尼尔的回答 是正确的,但如果你想它会在页面加载时自动播放,您可以将其添加到变量声明中:
Neil's answer is correct, but if you want it to automatically play when the page loads, you can add this to your variable declarations:
var auto = setInterval(moveRight, 1000); // 1000 ms = 1 sec
像这样:
var slideCount = $('#slider ul li').length;
var slideWidth = $('#slider ul li').width();
var slideHeight = $('#slider ul li').height();
var sliderUlWidth = slideCount * slideWidth;
var auto = setInterval(moveRight, 1000); // 1000 ms = 1 sec
这篇关于如何使此滑块自动播放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使此滑块自动播放?
基础教程推荐
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 动态更新多个选择框 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01