My jQuery slider is working but I don't know why it doesn't stop when I hover it.
I'm not using any slider plugin for this.
Here is the code.
$(function(){
var width = 720;
var animationSpeed = 1000;
var pause = 3000;
var currentSlide = 1;
var $slider= $('#slider');
var $slideContainer = $slider.find('.slides');
var $slides = $slideContainer.find('.slide');
var interval;
function startSlider(){
setInterval(function(){
$slideContainer.animate({'margin-left': '-='+width}, animationSpeed, function(){
currentSlide++;
if(currentSlide === $slides.length){
currentSlide = 1;
$slideContainer.css('margin-left', 0);
}
});
}, pause);
}
function stopSlider(){
clearInterval(interval);
}
$slider.on('mouseenter', stopSlider).on('mouseleave', startSlider);
startSlider();
stopSlider();
});
Any help would be appreciated.
Try this ;)
var width = 720;
var animationSpeed = 1000;
var pause = 3000;
var currentSlide = 1;
var interval;
var $slider = "";
var $slideContainer = "";
var $slides = "";
function startSlider(){
interval = setInterval(function(){
$slideContainer.animate({
'margin-left': '-=' + width
},
animationSpeed, function(){
currentSlide++;
if(currentSlide === $slides.length){
currentSlide = 1;
$slideContainer.css('margin-left', 0);
}
});
}, pause);
}
function stopSlider(){
clearInterval(interval);
}
$(function(){
$slider = $('#slider');
$slideContainer = $slider.find('.slides');
$slides = $slideContainer.find('.slide');
$slider.on('mouseenter', stopSlider).on('mouseleave', startSlider);
startSlider();
});
Man you do it wrong.
To stop an interval you need to assign it to a variable before.
Create your interval and assign it to var:
var timer = setInterval(function (){
// do stuff here
});
You have your time setted up, now you can stop it! Like this:
clearInterval(timer);
Hope to help you :)
if you want to Stop the interval you need to do it like:
var interval = setInterval(function(){//do stuff here});
clearInterval(interval);
Just remove $ from your variable prefix, it is not php, it is jquery.
Related
I am trying to make this jQuery slider when it's on the last slide it comes back to the first one, but I can't manage to do the same on the other side.
The code: http://jsfiddle.net/uctr94ve/1/
'use strict';
$(function() {
//settings for slider
var width = 720;
var animationSpeed = 1000;
var pause = 2000;
var currentSlide = 1;
//cache DOM elements
var $slider = $('#slider');
var $slideContainer = $('.slides', $slider);
var $container = $('.container');
var $sliderprevious = $('.sliderprevious', $container);
var $slides = $('.slide', $slider);
var interval;
function startSlider() {
(slide1, pause);
}
function slide1() {
$slideContainer.animate({
'margin-left': '+=' + width
}, animationSpeed, function() {
if (++currentSlide === $slides.length) {
currentSlide = 0;
$slideContainer.css('margin-left', 3600);
}
});
}
function pauseSlider() {
clearInterval(interval);
}
$sliderprevious
.on('mouseenter', pauseSlider)
.on('mouseleave', startSlider)
.on('click', slide1);
startSlider();
});
I want that this slide still plays and don't stop. Like an automatic loop. So what is the best code to do that? If you want to edit it, here's theJsFiddle
Here's the JS code:
var s = 0,
t = 2000;
$(document).on('ready', slide);
function slide() {
var speed = setTimeout(slider, t, );
}
function slider() {
s++;
var sld = $('#slider li'),
imgs = sld.length;
if (s == imgs) {
s = 0;
}
sld.eq(s - 1).animate({
'left': '0px'
}, t, function() {
sld.eq(s).animate({
'left': '0px'
}, t, function() {
speed = setTimeout(slider, t);
});
});
}
Change your JavaScript to this:
var s = 1, t = 3000;
$(document).on('ready', slider);
function slider(){
var sld = $('#slider li'),
imgs = sld.length;
if(s == imgs){
$('#slider li').css('left', '');
s = 1;
}
sld.eq(s).animate({'left': '0px'}, t, function(){
setTimeout(slider, 1000);
});
s++;
}
And a JSFiddle for you.
i am trying to get a div to slide down about 200px down a page using javascript when the user clicks another div which acts as a close button.
can someone please show me how i would do this?
<script>
$('.chat_close').click(function(e){
$('.chat_box').slideDown();
});
</script>
Someone already asked the same question a while ago. Avinash replied:
var minheight = 20;
var maxheight = 100;
var time = 1000;
var timer = null;
var toggled = false;
window.onload = function() {
var controler = document.getElementById('slide');
var slider = document.getElementById('slider');
slider.style.height = minheight + 'px'; //not so imp,just for my example
controler.onclick = function() {
clearInterval(timer);
var instanceheight = parseInt(slider.style.height); // Current height
var init = (new Date()).getTime(); //start time
var height = (toggled = !toggled) ? maxheight: minheight; //if toggled
var disp = height - parseInt(slider.style.height);
timer = setInterval(function() {
var instance = (new Date()).getTime() - init; //animating time
if(instance <= time ) { //0 -> time seconds
var pos = instanceheight + Math.floor(disp * instance / time);
slider.style.height = pos + 'px';
}else {
slider.style.height = height + 'px'; //safety side ^^
clearInterval(timer);
}
},1);
};
};
You can see it here at this URL:
http://jsbin.com/azewi5
Use animate
Also, use .on
<script type="text/javascript">
$('.chat_close').on('click', function(e){
$('.chat_box').animate({"top": "200px");
});
</script>
HTML:
<div class="chat_close">
</div>
I first do this and it works:
$(document).ready(function() {
var delay = 0;
var myLeft = $("#container").offset().left;
var myRight = myLeft + $("#container").outerWidth();
var newWidth = $("#container").width();
$("#head").animate({width: newWidth});
$("#head").css("marginLeft", myLeft).css("marginRight", myRight);
$('.box').each(function() {
$(this).delay(delay).fadeIn();
delay += 250;
});
});
Then i run this but it does't work, ideally it would have to work on each window resize:
function doSomething() {
var myLeft = $("#container").offset().left;
var myRight = myLeft + $("#container").outerWidth();
var newWidth = $("#container").width();
$("#head").animate({width: newWidth});
$("#head").css("marginLeft", myLeft).css("marginRight", myRight);
};
var resizeTimer;
$(window).resize(function() {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(doSomething, 100);
});
But it looks like as the new sizes are not applied each time there is a window resize, anyone?
Try
$("#head").css("margin-left", myLeft + "px").css("margin-right", myRight + "px");
You also seam to be using an unnecessary "each" clause as you're not doing anything for each box, only on #head. Are you sure you mean that?
window resize was happening before the new values were set therefore the trick was to include a timer delay for the window resize event:
var delay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
});
$(window).resize(function() {
delay(function(){
if ($("#head").width() != $("#container").width()){
var myLeft = $("#container").offset().left;
var newWidth = $("#container").innerWidth();
var myRight = $(window).width() - ($("#container").offset().left + $("#container").outerWidth());
$("#head").animate({ width: newWidth, marginLeft: myLeft, marginRight: myRight }, "fast");
}
}, 1000);
});
I'm new to Stack Overflow, so excuse me if I'm completely doing something I shouldn't be doing, and let me know so I can learn.
Anyway! The code:
$(document).ready(function() {
var imgWidth = $(".window").width();
var imgSize = $(".image_reel img").size();
var imgReelWidth = imgWidth * imgSize;
$(".image_reel").css({
width: imgReelWidth});
var num = 960;
var numImgs = $('div.image_reel img').length;
var currentSlide = 0;
setInterval(function() {
currentSlide++;
if (currentSlide != numImgs) {
$(".image_reel").animate({
left: -num
}, 1000);
}
else {
var setWidth = numImgs - 1;
var newSlideNum = num * setWidth;
$(".image_reel").animate({
left: newSlideNum
}, 1000);
currentSlide = 0;
}
}, 2000);
});
What this code is supposed to be doing (or at least I thought it was...) is that after 2 seconds, it will loop through the if statement and check if the "current slide" is equal to the amount of images there are. Now I checked with the alert function to see if all the numbers are correct and they are, but for some reason, the slider is only working once, and that's it. Any ideas why?
Thanks in advance guys.
This might be your problem:
var numImgs = $('div.image_reel img').length;
This holds the number of images at the moment it's called, not every time you call it. Replace numImgs with $('div.image_reel img').length in your code, and see if it works:
$(document).ready(function() {
var imgWidth = $(".window").width();
var imgSize = $(".image_reel img").size();
$(".image_reel").css('width', imgWidth * imgSize);
var num = 960;
var currentSlide = 0;
var setWidth = 0;
var newSlideNum = 0;
setInterval(function() {
currentSlide++;
if (currentSlide < $('div.image_reel img').length) {
$(".image_reel").animate({left: -num}, 1000);
} else {
setWidth = numImgs - 1;
newSlideNum = num * setWidth;
$(".image_reel").animate({left: newSlideNum}, 1000);
currentSlide = 0;
}
}, 2000);
});