Custom jQuery slider for html elements not working - javascript

I'm trying to add a slider to animate some paragraphs at my home screen.
I don't want to use any plugin.
I want to create it myself using jQuery. But there is a problem, it seems that condition in jQuery is not working.
Please checkout following codes and try to fix it.
<div id="slider-viewport">
<div class="slider">
<p>1 Cloud based e-commerce solution for your downloadable products</p>
<p>2 Cloud based e-commerce solution for your downloadable products</p>
<p>3 Cloud based e-commerce solution for your downloadable products</p>
<p>4 Cloud based e-commerce solution for your downloadable products</p>
</div>
</div>
CSS
.slider p {
font-size: 25px;
color: #fff;
height: 100px;
margin: 0;
}
#slider-viewport {
width: 100%;
height: 100px;
overflow: hidden;
background-color: black;
}
.slider {
width: 100%;
height: auto;
}
jQuery
$(document).ready(function() {
$('.slider p').first().clone().appendTo('#slider-viewport .slider')
function slider() {
var $slider = $('#slider-viewport .slider');
var currentMargin = $slider.css('margin-top');
var paraHeight = $('.slider p').height();
var setMargin = parseInt(currentMargin) - paraHeight;
var resetMargin = -300;
if (currentMargin < resetMargin) {
$slider.css('margin-top', 0);
};
$slider.animate({
marginTop: setMargin
}, {
duration: 600,
easing: "easeOutQuint"
}
);
};
setInterval(slider, 3000);
});

You can simplify things a bit by animating the first element only and then resetting and putting it back at the end of the list after the animation, also you can avoid setInterval() by making the function recursive
function slider() {
var $slider = $('#slider-viewport .slider');
var $first = $slider.find('p:first');
$first.delay(3000).animate({'margin-top': $first.height() * -1}, 600, "easeOutQuint", function () {
$(this).css('margin-top', 0).appendTo($slider);
slider();
});
};
$(document).ready(function () {
slider();
});
Demo fiddle

var currentMargin = $slider.css('margin-top').split('px')[0];
$slider.css('margin-top', '0px');
Update
$(document).ready(function() {
setInterval(slider, 3000);
});
var $slider = $('#slider-viewport .slider');
var marginNew = $('.slider p').height();
var limit=marginNew*$('.slider p').length;
function slider() {
if(marginNew>=limit)
marginNew=0;
$slider.animate({"margin-top":-marginNew});
marginNew+=100;
};
http://jsfiddle.net/Xh9SM/2/

Fixed Here
http://jsfiddle.net/sk5wq/4/
html code:
<div id="slider-viewport">
<div class="slider">
<p>1 Cloud based e-commerce solution for your downloadable products</p>
<p>2 Cloud based e-commerce solution for your downloadable products</p>
<p>3 Cloud based e-commerce solution for your downloadable products</p>
<p>4 Cloud based e-commerce solution for your downloadable products</p>
</div>
</div>
css code:
.slider p {
font-size: 25px;
color: #fff;
height: 100px;
margin: 0;
}
#slider-viewport {
width: 100%;
height: 100px;
overflow: hidden;
background-color: black;
}
.slider {
width: 100%;
height: auto;
}
update js code:
$(document).ready(function() {
setInterval(initSlider, 3000);
});
function initSlider() {
var slider = $('#slider-viewport .slider'),
sliderMargin = parseInt(slider.css('margin-top')),
pHeight = parseInt(slider.find('p').height()),
sliderGep = '-'+(parseInt(slider.height())-pHeight),
go = 0;
if (sliderMargin <= sliderGep) {
slider.animate({marginTop:pHeight+'px'},0);
go = '0px'
}else{
go = '-='+pHeight+'px'
}
slider.animate({
marginTop: go
}, {
duration: 600,
easing: "easeOutQuint"
});
}

Related

Why animate's callback function (complete) executed at start?

Duplicate:
Yes it is but a little bit different from this.
I'm using jQuery 3.1.0 This is my code:
$(document).ready(function () {
ToggleAsideBar();
});
function ToggleAsideBar(){
$(".ah-sidebar-toggle").click(function(){
let toggleSize = $(".ah-logo").width() == 230 ? "50px" : "230px";
$(".ah-logo").animate(
{ width: toggleSize },200,"linear",function () {alert("Animation Complete!");'}
);
});
}
Problem:
In my case, alert show before the animation starts. I want to show alert after the animation completed. Any thoughts what I am doing wrong in above code?
Live Example:
jsfiddle
You had a collision between jquery's animation and css's transition.
If you decide to use animate - you should remove the transition lines from your css file.
Here is a working version (without the transition in your css):
$(document).ready(function () {
ToggleAsideBar();
});
function ToggleAsideBar(){
//Get width of browser
$(".ah-logo").click(function(){
let toggleSize = $(".ah-logo").width() == 230 ? "50px" : "230px";
$(".ah-logo").animate(
{ width: toggleSize },200,"swing"
).promise().done(function(){
alert("Animation Complete!");
});
});
}
.ah-logo {
width: 230px;
float: left;
font-weight: 600;
font-size: 16px;
text-align: center;
overflow: hidden;
}
a {
line-height: 50px;
display: block;
}
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
<div class="ah-logo">
Admin
</div>

JavaScript Firefox slow animation

For demonstration purposes provided example I tried to implement animation in which one div follows actual scrollbar in order to get as many changes as possible.
However, animation works really nice on Chrome, pretty well even in IE but it is extremely laggy in Firefox.
I tried with some hardware acceleration tricks like translateZ(0), etc, but with no success for Firefox.
Instead of top I tried to use transform, again, no luck.
Can I expect better performance if I try canvas?
(function () {
var scrollerElement = document.getElementById('scroller');
var elementToMove = document.getElementById('toMove');
var scrollerSize = scrollerElement.clientHeight;
var maxScroll = scrollerElement.scrollHeight - scrollerSize;
var whileAnimate;
var timeout;
var delay = 200;
scrollerElement.addEventListener('scroll', function (event) {
clearTimeout(timeout);
timeout = setTimeout(cancelAnimation, delay);
if (whileAnimate) {
return;
}
startAnimation();
});
function cancelAnimation() {
whileAnimate = false;
}
function startAnimation() {
var animationFrame;
whileAnimate = true;
animate();
function animate() {
if (!whileAnimate) {
cancelAnimationFrame(animationFrame);
return;
}
elementToMove.style.top = (scrollerElement.scrollTop / maxScroll * scrollerSize) + 'px';
animationFrame = requestAnimationFrame(animate);
}
}
})();
body {
margin: 0;
padding: 0;
}
#scroller {
width: 500px;
height: 500px;
border: 1px solid black;
overflow: auto;
}
#content {
width: 500px;
height: 20000px;
}
#toMove {
position: absolute;
top: 0;
width: 20px;
height: 20px;
background: red;
}
<div id="scroller">
<div id="toMove"></div>
<div id="content"></div>
</div>

how to create a sliding menu which will fill the window width except by puller button width?

I am trying to create a sliding menu which will fill the window width minus the puller button width. I want the menu out of the window when web page load and there is a button which will go left when the page loads. When user clicks on the button the menu should come to window and button goes right.
I have created an animation video here which will show you what I want.
In the jsfiddle the menu just fades out, But I want the menu to slide from left to right when clicked on button with .puller class.
See the code at jsfiddle
Demo on jsfiddle
$(document).ready(function() {
var stickyNavTop = ($('.container')).offset().top;
var stickyNav = function(){
var scrollTop = $(window).scrollTop();
if (scrollTop > stickyNavTop) {
$('.container').addClass('fixed');
} else {
$('.container').removeClass('fixed');
}
};
stickyNav();
$(window).scroll(function() {
stickyNav();
}).resize(function() {
stickyNav();
}).load(function() {
stickyNav();
});
$(".dropdown").click(function() {
$(".dpitem").slideToggle(300);
});
var calcWidth = function() {
var pullerDimensions = $('.puller').width();
$('#cpcc,.dpitem').width($(window).width() - pullerDimensions);
}
$(document).ready(function() {
calcWidth();
});
$(window).resize(function() {
calcWidth();
}).load(function() {
calcWidth();
});
var cPcc = document.getElementById ("cpcc");
var cpHeight = function() {
var cpBtnHolder = $(cPcc).height();
$('.content').css("padding-top",cpBtnHolder);
}
setInterval(function() {
cpHeight();
calcPHeight();
pp();
}, 0)
var calcPHeight = function() {
var toolsDimensions = $('#cpcc').height();
$('.puller').height(toolsDimensions);
$('.puller').css("line-height",toolsDimensions +"px");
}
$(document).ready(function() {
calcPHeight();
});
$(window).resize(function() {
calcPHeight();
}).load(function() {
calcPHeight();
});
var Pwidth = $('#cpcc').width();
var PSpce = $(window).width()-Pwidth;
$(".puller").click(function() {
$(".container").toggle( function() {
$(".container").css({
transform: "translate3d("+"-"+Pwidth+"px, 0, 0)"
});
}, function () {
$(".container").css({
transform: "translate3d(-0, 0, 0)"
});
});
});
});
Calc is your friend. You can use that along with 'transition' to achieve the intended effect. Check out this simplified example.
http://codepen.io/anon/pen/gPBQOb
Good practice is to toggle a class which will override the position property in this case left. It should also toggle your chevron image to point left and right.
Your example is using a lot of js calculations and has to recalculate with window size changes. This example uses js only to toggle a css class on click and the rest in managed purely with css. Much simpler in my opinion.
HTML
<div class="maincontent"></div>
<div class="menu">
<div class="button"></div>
</div>
CSS
.maincontent{
background-color: green;
height: 2000px;
width: 100%;
}
.menu{
background-color: red;
width: 100%;
height: 200px;
position: fixed;
top: 0px;
left: 0px;
transition: left 1s;
}
.menu.collapse{
left: calc(-100% + 30px)
}
.button{
background-color: yellow;
height: 100%;
width: 30px;
position: absolute;
right: 0px;
}
JS
$('.button').click(
function(){
$('.menu').toggleClass('collapse')
})

Count back percentage using JQuery

I have the code below where I'd like to the numbers count back to 0% once hover the object out. Also I can't figure our how to make the value disappear again as it was on load. Could you please help me solve this.
Thanks in advance.
HTML
<div class="container">
<div class="fill" data-width="80%"></div>
</div>
<div class="container">
<div class="fill" data-width="50%"></div>
</div>
CSS
.container {
position: relative;
width: 300px;
height: 30px;
background-color: blue;
margin: 10px auto;
}
.fill {
height: 100%;
width: 0;
background-color: red;
line-height: 30px;
text-align: left;
z-index: 1;
text-align: right;
}
JQuery
$(function() {
$('.container').hover( function(){
var width=$(this).find(".fill").data('width');
$(this).find(".fill").animate({ width: width }, {
duration:800,
step: function(now, fx) {
$(this).html(Math.round(now) + '%');
}
});
},
function(){
$(this).find(".fill").animate({ "width": "0px" }, 800);
});
});
jsFiddle http://jsfiddle.net/zp8pe069/
jsBin demo
CSS: set overflow: hidden to .fill to prevent the text being visible after the animation ends.
HTML: remove % from the data attribute
JS and here you go. all you need:
$('.container').hover(function( e ){
var $fill = $(this).find(".fill");
var width = $fill.data('width');
$fill.stop().animate({width: e.type=="mouseenter" ? width+"%" : "0%" }, {
duration : 800,
step : function(now) {
$(this).html(Math.round(now) + '%') ;
}
});
});
Note also the use of the .stop() method, if you hover multiple time hysterically :) it'll prevent endless animations.

JQuery doesn't appear to be working in Wordpress

I have the following in the tags of the header in my wordpress theme, but no matter what it will not work.
<script>JQuery(function() {JQuery('#fader img:not(:first)').hide();JQuery('#fader img').css('position', 'absolute');JQuery('#fader img').css('top', '0px');JQuery('#fader img').css('left', '50%');JQuery('#fader img').each(function() {var img = JQuery(this);JQuery('<img>').attr('src', JQuery(this).attr('src')).load(function() {img.css('margin-left', -this.width / 2 + 'px');});});var pause = false;function fadeNext() {JQuery('#fader img').first().fadeOut().appendTo(JQuery('#fader'));JQuery('#fader img').first().fadeIn();}function fadePrev() {JQuery('#fader img').first().fadeOut();JQuery('#fader img').last().prependTo(JQuery('#fader')).fadeIn();}JQuery('#fader, #next').click(function() {fadeNext();});JQuery('#prev').click(function() {fadePrev();});JQuery('#fader, .button').hover(function() {pause = true;},function() {pause = false;});function doRotate() {if(!pause) {fadeNext();}}var rotate = setInterval(doRotate, 2000);});</script>
What a mess right? To show that I have been researching this, a previous answer said to remove all whitespaces and change all $ with JQuery. I want to simply fade between a few images in the fader ID.
I have even made a fade.js file, and included it in the functions file, to no avail.
Here is my CSS...
#fader {
position: relative;
width: 100%;
height: 400px;
}
.button {
background-color: green;
width: 50px;
height: 30px;
font-size: 20px;
line-height: 30px;
text-align: center;
position: absolute;
top: 30px;
}
#next {
right: 100px;
}
#prev {
left: 100px;
}
And my HTML ...
<div id="fader">
<img src="http://dummyimage.com/600x400/000/fff.png&text=Image1"/>
<img src="http://dummyimage.com/200x400/f00/000.jpg&text=Image2"/>
<img src="http://dummyimage.com/100x100/0f0/000.png&text=Image3"/>
<img src="http://dummyimage.com/400x400/0ff/000.gif&text=Image4"/>
<img src="http://dummyimage.com/350x250/ff0/000.png&text=Image5"/>
</div>
Instead of showing one image and fading between the 5 images supplied, it just shows all 5 images at the same time.
I first thought this could be a JQuery problem, so tested to see if JQuery was functioning, which it is.
Here is the original JQuery before I removed whitespaces and swapped $ for JQuery.
$(function() {
$('#fader img:not(:first)').hide();
$('#fader img').css('position', 'absolute');
$('#fader img').css('top', '0px');
$('#fader img').css('left', '50%');
$('#fader img').each(function() {
var img = $(this);
$('<img>').attr('src', $(this).attr('src')).load(function() {
img.css('margin-left', -this.width / 2 + 'px');
});
});
var pause = false;
function fadeNext() {
$('#fader img').first().fadeOut().appendTo($('#fader'));
$('#fader img').first().fadeIn();
}
function fadePrev() {
$('#fader img').first().fadeOut();
$('#fader img').last().prependTo($('#fader')).fadeIn();
}
$('#fader, #next').click(function() {
fadeNext();
});
$('#prev').click(function() {
fadePrev();
});
$('#fader, .button').hover(function() {
pause = true;
},function() {
pause = false;
});
function doRotate() {
if(!pause) {
fadeNext();
}
}
var rotate = setInterval(doRotate, 2000);
});
Thanks in advance!!!!!!!
I love you ;)
Use jQuery or $ and not what you are using.
You are using JQuery, which is wrong.
But if you really want to use JQuery,then you can do the below:
var JQuery = jQuery;
Now, after the assignation, you can use JQuery

Categories