Jquery图像滑块自动播放 [英] Jquery image slider auto play
本文介绍了Jquery图像滑块自动播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从我的滑块jquery部分添加一个自动播放。我能做什么?
I want to add an autoplay from my slider jquery section. What can i do this ?
我有下一个&一个上一个按钮,但我想添加一个自动播放,当鼠标悬停在幻灯片自动停止的图像上时。
I have a next & a prev button but I want to add an auto-play and also when mouse hover over image that slide automatically stopped.
任何人都可以帮助我吗?
Anyone can help me here ?
这是我的 DEMO 来自codepen的页面
This is my DEMO page from codepen
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();
});
});
推荐答案
像这样修改你的脚本:
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
}, 1000, function () {
$('#slider ul li:last-child').prependTo('#slider ul');
$('#slider ul').css('left', '');
});
};
function do_slide(){
interval = setInterval(function(){
moveLeft();
}, 1000);
}
do_slide();
$('ul li').hover(function(){
clearInterval(interval);
});
$('ul li').mouseleave(function(){
do_slide();
});
});
这篇关于Jquery图像滑块自动播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文