Velocity js translateY issues - javascript

I have following code to do transitionx
var w = $(window).width()*0.02;
$('#mChangePassword').velocity({translateX: w},
{
duration: 300,
'complete': function(){
$('.mSettingsMain').fadeOut(300);
}
});
Attached GIF how it works in mobile app
http://g.recordit.co/WDCGVFPXsE.gif
NOTE: Clicking on the above link is not working somehow, copy pasting image links works.
Here the animation is coming from right to left when I click on button.
I want same animation from bottom to top, I tried using translateY but it is not working,
I tried with the following code
var h = $(window).height()*0.02;
$('#mChangePassword').velocity({translateY: h},
{
duration: 300,
'complete': function(){
$('.mSettingsMain').fadeOut(300);
}
});
but it is not working, I want the same animation from that gif "bottom to up" or "slide up".
Any help appreciated. Thanks.

When transitioning transforms in Velocity it's best to use forcefeeding and provide units to the property values:
$('#mChangePassword').velocity({translateY: [h + 'px', '100%']});

Related

JQuery - IF statement only works two to three times

I'm trying to make this page working that has a vertical line of pictures (thumbnail-sized) which you can then click on to make them scroll to full width and show lots of text and happy stuff.
I've already managed to do all that. However, if I click on one thumbnail, any open thumbnails are supposed to close. I've implemented an IF statement and class-checking for that:
$("a#shins-rec1").clickToggle(
function(){
if ($(".shins-rec").hasClass('done')) {
$(".shins-rec").filter($(".done").not($(this))).animate({
left: $(".shins-rec").filter($(".done").not($(this))).width() / 2 - 60,
width: '120px'}, 250, function(){
$(this).removeClass('done');
});
} else {
$(this).parent().animate({left: 0, width:'100%'}, 250, function(){
$(this).addClass('done')
});
};
},
function(){
$(this).parent().animate({
left: $(this).parent().width() / 2 - 60,
width: '120px'}, 250, function(){
$(this).removeClass('done')
}); // even clicks
});
which works all fine, but after you clicked two or three times on different thumbnails to open them and close the other ones, the already open thumbnail gets stuck and the clicked thumbnail opens as it should open. You have to click on the clicked thumbnail to close the other one, and then you have to wait some time till you can click the clicked thumbnail to properly close it again. After that it just stays like this.
I've already tried doing it with a plugin called Summer of GOTO, because I think it has something to do with checking whether there are any "done" classes or not, so I wanted to make it re-check stuff using goto in JQuery. Using a WHILE statement didn't work either, but that could be because I don't understand that function.
Anyone knows how to solve this?
Thanks in advance.
EDIT: : JSfiddle of what I'm working on
It looks like the click even which you fire on each thumbnail has same ID, ID of the element suppose to be unique, you can use class instead like this
$("a.shins-rec1").clickToggle(
function(){
if ($(".shins-rec").hasClass('done')) {
$(".shins-rec").filter($(".done").not($(this))).animate({
left: $(".shins-rec").filter($(".done").not($(this))).width() / 2 - 60,
width: '120px'}, 250, function(){
$(this).removeClass('done');
});
} else {
$(this).parent().animate({left: 0, width:'100%'}, 250, function(){
$(this).addClass('done')
});
};
},
function(){
$(this).parent().animate({
left: $(this).parent().width() / 2 - 60,
width: '120px'}, 250, function(){
$(this).removeClass('done')
}); // even clicks
});
I've finally gotten the answer to my problem, someone on another forum kind of rewrote my code because it was messy and things were repetitive and they solved the stuckness.
Here is the JSfiddle in case anyone wants to look into it.
For some reason the orientation of the thumbnails changed which made a 0 value for "left" position the thumbnail in the center and a 240 or similar made it shift to the right. But I simply changed that by switching the left-values and changing the 240 or similar into a negative number, as seen below.
var rightPos = -1 * ($('div.content-ins-used').width() / 2 - 60);
Thanks anyway for your help!

jQuery .animate() acceleratation?

I have searched for an answer before this, but like most of my problems, I find nothing. :(
Basically I'm trying to make a custom jQuery image slider using a basic system:
Put all image paths into an array.
Pre-load all images using each array entry path.
Load HTML 'img's (left, center and right) with the array paths [0],1 and [2].
When you click left, all the images margin-left move to left by 100% (and vice versa for right)
Here's my problem; When I click one of the navigation buttons, the images move but NOT at the same speed, there's some kind of acceleration factor I can't seem to turn off, meaning there is a gap between the images when they move.
Anyone know why this happens and how to stop it?
I have another issue as well regarding this, should I make a separate question for this?
Here's the Javascript.
$(document).ready(function(){
var images = [ "http://furrydex.net/img1.png", "http://furrydex.net/img2.png" ];
var loadedImages = [];
var pos = 0;
$.each(images, function( index, value ) {
var img=new Image();
img.src=value;
loadedImages.push(img);
});
if(images[2] == undefined) {
$('#left').attr('src', images[1]);
$('#center').attr('src', images[0]);
$('#right').attr('src', images[1]);
}
$('.navigationLeft').click(function() {
console.log("yay");
$( '.slide' ).animate({
marginLeft: "100%"
}, 500, "linear", function() {
// Animation complete.
});
});
});
The left black block is 'left' which you need to test it.
Here's the JSFiddle
Ok, I put something up, that makes it gapless, but I do think finding the culprit for making it somehow percentage-based-failing would be the right trick. ;)
var selWidth = $('.selector').width();
$( '.slide' ).animate({
marginLeft: selWidth
})
JSFIDDLE: http://jsfiddle.net/TRNCFRMCN/RrQ5d/5/

Slide issues with my image slider on my website

Hey so the problem I am having is on my website www.stallionstride.org .
The image slider works when my buttons are pressed slowly, however, if you press the left and right buttons quickly when my images should slide back the other way instead it slides to an empty spot.
I tried using the stop command but that has not worked for me.
Here is my code. I only included the right buttons event since the left is basically the same. Any help is greatly appreciated.
$('#rightbutton').click(function() {
//clear the timer since photos scroll through at a certain amount of time
clearTimeout(timeoutTracker);
var scrollAmount = $('#slideshow_inner img').width();
var currentPos = Math.abs(parseInt($('#slideshow_inner').css('left')));
var remainingScroll = ($('#slideshow_inner').width() - currentPos)/908;
if (remainingScroll == 1) {
$('#slideshow_inner').stop(true, false).animate({ 'left' : '0' }, 'slow');
}
else {
$('#slideshow_inner').stop(true, false).animate({ 'left' : '-=' + scrollAmount}, 'slow');
}
timeoutTracker = setTimeout(function() {$('#rightbutton').click(); }, 3000);
});
Sliding images requires more design especially with back/next button.
I would suggest well developed plugin instead of building on your own.
One I can think of is slidejs, http://www.slidesjs.com/

jQuery: custom horizontal accordion width issue

Im trying to create a custom horizontal accordion style showcase. In terms of actual functionality, i have the framework (which can be seen here):
http://www.jsfiddle.net/adrianjacob/UdUus/
However my main bug bear (and the clients) is that if you look at the right hand side, there is always slight movement/flickering as the widths animate up and down.
Ideally I want it to appear smooth so only the opening/closing lists have movement.
ANy advice would be much appreciated.
A.
Use the animate function's step (it's not well documented)... I've updated the demo
var panels = $('#promo li');
panels.hoverIntent(
function() {
if (!$(this).is('.expanded') && !panels.is(':animated')) {
$(this).animate({
width: 200
}, {
// width is the calculated width, ani is the animation object
step: function(width, ani) {
var w = Math.floor(width);
// use 250 so we end up with 50 as the reduced size
$('.expanded').css('width', (250 - w) + 'px');
$(ani.elem).css('width', (200 - w) + 'px');
},
duration: 500,
complete: function() {
panels.removeClass('expanded');
$(this).addClass('expanded');
}
});
}
}, function() {});
A similar method is used in the Kwicks plugin.
You could try this plugin which may have figured out the bug. The example provided was too showy to actually tell.
Have you played around with jQuery UI easings?
You can also try to stop the event just when the div is opening.
The website with explanation is: http://api.jquery.com/stop/

Jquery Cycle + Firefox Squishing Images

I am running jQuery Cycle for an image gallery. View the link: Here
My problem is that the images are getting squished when viewed in firefox. The problem disappears when I re-load the page. This leads me to believe that the Javascript is triggering before all the images are loaded (usually the first image works fine and the rest are squished.)
A hard re-fresh reproduces the problem.
I've wrapped everything in a $(document).ready(function(){ }); but it still happens.
Additional Info: If I specify the width and height of the image, everything works fine. However there are hundreds of images all at different sizes..
I'm pretty frustrated with this problem. Any ideas/help is greatly appreciated!
Here is my code:
$(document).ready(function(){
//function onBefore(curr,next,opts) {
// var $slide = jQuery(next);
// var w = $slide.outerWidth();
// var h = $slide.outerHeight();
// $slide.css({
// marginTop: (482 - h) / 2,
// marginLeft: (560 - w) / 2
// });
//};
// Decare the function that center the images...
function onBefore(curr,next,opts) {
var $slide = jQuery(next);
var w = $slide.outerWidth();
var h = $slide.outerHeight();
$slide.css({
marginTop: (480 - h) / 2,
marginLeft: (560 - w) / 2
});
};
$(document).ready(function() {
$('#slideshow').cycle({
fx: 'fade',
next: '#next',
pause: 0,
speed: 500,
before: onBefore,
prev: '#prev',
pause: '#pause',
pager: '.thumbs',
pagerClick:function(zeroBasedSlideIndex, slideElement) {$(slideElement).find('div.cover').hide();},
pagerAnchorBuilder: function(idx, slide) {
var src = $('img',slide).attr('src');
//Change height of thumbnail here
return '<li><img src="' + slide.src + '" height="90" /></li>';
}
});});});
There is a much simpler and cleaner solution that I used to solve this problem than what has already been proposed:
Using jQuery, you need to use $(window).load instead of $(document).ready for your particular situation. To fix the issue, change this:
$(document).ready(function() {
$('#slideshow').cycle({
/* ... */
});
});
To this:
$(window).load(function() {
$('#slideshow').cycle({
/* ... */
});
});
Why does this work? Because window.onload fires after all referenced images on the page are loaded (See https://developer.mozilla.org/en/DOM/window.onload, and .load() - jQuery API), which is the desired behavior in your situation. $(document).ready, better known as "DOM Ready", will fire before images have loaded. This is typically the desired behavior, but in your situation it's too early.
I had the same problem when working on a site several months ago (linked below). If you're starting cycle in $(document).ready(), here's what happens when a client browses to your page:
1) The client's browser sends a request for each img element. Those requests take variable amounts of time to fulfill.
2) Before the image requests are completed, cycle starts. Cycle works by hiding all but the first image in the slide show: it sets visibility:hidden and display:none on each of its images.
The problem is that Firefox fixes the img element's size once and for all at the point the display style is set to none. So if the image hasn't finished loading, its height and width style attributes are small (I'm not sure exactly what they correspond to - perhaps the size of Firefox's image placeholder). When cycle shows the image by setting its style attribute to display:block, it uses whatever dimensions it had at the time it was hidden.
I solved this by changing my code so that it doesn't start the cycle plugin until all the images are finished loading. To do that, I initialize a counter variable to the number of images I'm cycling, then bind a load event to each image like this:
var imagesRemaining = 12; // 12 is just the number of images in the slideshow div
$(document).ready(function() {
$('#slideshow > img').bind('load', function(e) {
imagesRemaining = imagesRemaining - 1;
if (imagesRemaining == 0) {
// I'm doing some other stuff when initializing cycle
startCycle();
// My images all start with visibility:hidden so they don't show
// before cycle hides them in a 'stack', so ...
$('#slideshow > img').css('visibility', 'visible');
}
});
});
function onBefore(curr, next, opts) { // Your code here ... }
function startCycle() {
$('#slideshow').cycle({ ... // your initialization here });
}
You can see it in action by viewing the galleries on this site in Firefox. I'm building the gallery pages dynamically, so it's structured a bit differently than your page, but you can see more details if you poke around with Firebug.
I'd also like to add that it seems adding a width and height attribute solves this problem.
Ok i know its probably an awfull way of calling load but i just coulnd bind my cycle code to .load for some reason it just don't work so i called the whole Cycle initializer inside the ...
i couldn't force the sizes since i'm cycling through li containing dynamic images and data
its probably flawed at some extend but for those as desperated as me...
Josh, your solution has just saved me a headache, thank you very much!
I think i've amended it slightly in order to handle pages where you don't know the total number of images. It seems to be working fine for me, if anyone can see any flaws, please point them out - i'm still learning.
$(document).ready(function () {
$('#slideshow > img').each(
function go() {
$(this).bind('load', function (e) {
projects();
$('#slideshow > img').css('visibility', 'visible');
});
});
});
function projects() {
$('#slideshow').cycle({
fx: 'scrollHorz',
speed: 300,
timeout: 0,
next: '#next ',
prev: '#prev ',
after: onAfter,
nowrap: 1,
autostop: 1
});
}
If you're using a database to populate the slideshow you could try accessing the image dimensions from the image itself.
For example, using django you can use
width="{{ xxx.image.width }}px" height="{{ xxx.image.height }}px"
in your img tag.
You can use a solution similar to making youtube videos responsive. You need to know the ratio of your width to height, and add that as padding-bottom to the cycling div. For my 1024X680 photos, I used 680/1024 = 66.4%
In your case, I believe
#slideshow{
padding-bottom:66.4%;
}
will show the image unshrunk. I have no idea what the actual height and width values you are working with, so substitute your own. I had to use this solution when the $(window).load solution proved maddeningly ineffective -- so now I use both.
This is better than setting the dimensions of the image, because it's slides into a fluid, responsive enviroment.

Categories

Resources