JavaScript Function To Trigger CSS3 Transition not working - javascript

Hi i am having problems with a transition of mine that gets called when a button is pressed. The reason i am confused is because it worked before but then i moved it onto a different div and now its not? when i mean its not working i mean that the function works (as in it does do something) and then runs the callback but when it switches the class' it doesn't 'animate' it. The div just stays there till the animation time is over and then runs the callback which hides it.
So basically it just hides it but doesn't animate the slide out effect that it should do.
the rest of the switchClass functions work fine even the one which makes the page slide back in.
The Code That Isn't Functioning Properly:
function hidepage() {
$( ".PageShow" ).switchClass("PageShow", "PageHide", loadpanel);
// Alternative $( "PageContainer" ).switchClass("PageShow", "PageHide", loadpanel);
};
And the rest of the code that goes with it...
CSS:
#PageContainer {
margin-top:120px;
width:100%;
}
.PageShow {
position:fixed;
-webkit-transform:translate(0px,0px);
-moz-transform:translate(0px,0px);
-ms-transform:translate(0px,0px);
-o-transform:translate(0px,0px);
transform:translate(0px,0px);
transition-duration: 0.3s;
-moz-transition-duration: 0.3s;
-webkit-transition-duration: 0.3s;
transition-delay: 0.1s;
-moz-transition-delay: 0.1s;
-webkit-transition-delay: 0.1s;
-o-transition-delay: 0.1s;
transition-timing-function: ease-out;
-moz-transition-timing-function: ease-out;
-webkit-transition-timing-function: ease-out;
-o-transition-timing-function: ease-out;
}
.PageHide {
position:fixed;
-webkit-transform:translate(-100%,0px);
-moz-transform:translate(-100%,0px);
-ms-transform:translate(-100%,0px);
-o-transform:translate(-100%,0px);
transform:translate(-100%,0px);
transition-duration: 0.5s;
-moz-transition-duration: 0.5s;
-webkit-transition-duration: 0.5s;
transition-timing-function: ease-out;
-moz-transition-timing-function: ease-out;
-webkit-transition-timing-function: ease-out;
-o-transition-timing-function: ease-out;
}

If you change your method .switchClass() to .removeClass() and .addClass() it will work. Look at this jsFiddle. There are two functions one is called .hidepage() which is the one with .removeClass and .addClass and the second function is .hidepagetwo() which uses .switchClass()
The real mistake is: there is a }); missing at the end, at least in jsFiddle you code works after adding this.

Looks like you are using switchClass incorrectly. Here is the API doc:
http://api.jqueryui.com/switchClass/
Note that the method takes 2 mandatory params and 3 optional. You can't stuff loadpanel(if that's your callback function) into the 3rd param because it belongs in the 5th. You need to specify values for the 3rd and 4th in this case so your callback function ends up where it's expected.
Also, You have a lot of CSS transition stuff going on, but my understanding is that switchClass does a javascript animation between your two classes. Look how the example is at the above link. You just need to specify the before style in one class and the after style in another. switchClass will interpolate the in-between and do the animation without all the CSS transitions.
Since you have all those CSS animations in place on classes, another approach is to try jQuery's toggleClass which simply turns on/off the classes: http://api.jquery.com/toggleClass/

Related

Add a fadeIn to a switch from hide to block display

I have a
<a onclick="document.getElementById('massage').style.display = 'block';">Button</a>
And I want to add a fadeIn to the function, but dont know how.
You have two options:
A css3 opacity animation - only suitable for HTML5 enabled browsers i.e. IE9+. Apply this by adding the css class fade to your element.
.fade {
opacity: 1;
transition: opacity .25s ease-in-out;
-moz-transition: opacity .25s ease-in-out;
-webkit-transition: opacity .25s ease-in-out;
}
2.) Use jquerys fadein function. Which supports older browsers.
http://api.jquery.com/fadeIn/
You will need to learn jQuery of course if you don't know it but this is worth doing I think.
Hope that helps

Easing animation in JavaScript without jQuery or other libraries

I want to have a <div> come into a webpage from the right when you hover over it. So something like this:
$('div.from_right').hover(function() {
$(this).animate({right: '-300px'});
});
But I don't want to use jQuery or any other libraries. Is there a built-in mechanism within JavaScript to animate the position? Or do I call requestAnimationFrame() repeatedly until the <div> is where I want it to be?
I only care about modern browsers.
Use CSS transition.
div {
width:100px;
height:100px;
background:red;
-webkit-transition:margin 3s ease-out;
-moz-transition:margin 2s ease-out;
-o-transition:margin 2s ease-out;
-ms-transition:margin 2s ease-out;
transition:margin 2s ease-out;
}
div:hover {
margin-left:300px;
}
DEMO.

Fading specific CSS/JS Popup when opening/closing

The following code snippet shows how I made popups with CSS and JS. Is there any chance to let it fade when opening/closing it, without changing the way I used to work, I mean just popping up the box by changing its display style?
function lightbox_open(){
window.scrollTo(100,500);
document.getElementById('light').style.display='block';
document.getElementById('fade').style.display='block';
}
function lightbox_close(){
document.getElementById('light').style.display='none';
document.getElementById('fade').style.display='none';
}
You could use either use jQuery to do the fading $().fadeIn() or use CSS3 animations if you want to stick to bare JS. In the latter case, set the opacity to 0 by default and change it to 1 via Javascript. You need to add this to your stylesheet:
selector {
opacity: 0;
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-ms-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
}
You can use jquery fadein() on open and fadeout() on close, but if you want to do it with pure javascript, i recommend reading this source code. they did it it wonderfully.
You can use jQuery:
.fadeIn()
function lightbox_open(){
window.scrollTo(100,500);
$('#light,#fade').fadeIn();
}
.fadeOut()
function lightbox_close(){
$('#light,#fade').fadeOut();
}

hidden header drop down on click

For a site I am working on I'd quite like to use a similar drop down effect as here http://shop.jack-hughes.com/ when you click info a hidden div drops down.
I can't work out if it uses only CSS3 or Javascript/CSS can anyone point me in the right direction or tell me the name of the effect; pretty simple I guess but for the life of me can't find another example.
combination of CCS3 and js
Here is what is used in the website you refer
js:
Event.observe(window, 'load', function () {
Event.observe('info', 'click', function () {
$('aside').toggleClassName('open');
});
});
Event.observe is from the prototype framework - http://prototypejs.org/doc/latest/dom/Event/observe/
The equivalent in jQuery(http://jquery.com/) for instance would be:
$(document).ready(function () {
$('.info').click(function () {
$('aside').toggleClass('open');
})
});
css:
aside.open {
height: 21.25em;
}
aside {
position: relative;
background-color: #3f4642;
width: 100%;
color: white;
letter-spacing: 0.1em;
-webkit-transition: all 0.3s ease-out;
-moz-transition: all 0.3s ease-out;
-ms-transition: all 0.3s ease-out;
-o-transition: all 0.3s ease-out;
transition: all 0.3s ease-out;
}
The HTML element is <aside>....</aside> but it's no difference if you choose div.
They have a small piece of Javascript that does this that can be easily done on any website. Basically you need a hidden div at the top of your page, and upon clicking a link you simply show the div.
The code that they used was:
Event.observe('info', 'click', function(){
$('aside').toggleClassName('open');
});
But if you take a look at jquery then you will see that manipulation of elements is quite easy to do.
One thing that they do use in addition is a CSS3 transition in their open class:
.aside {
transition: all 0.3s ease-out 0s
}
This is what is causing the smooth transition effect. So you can use either jQuery or the CSS3 transition, both give the same effect. I would say that the CSS3 transition is nicer, but then again you will be alienating certain browsers if they do not support transitions.
Probably using jQuery. Something like:
http://api.jquery.com/toggle/
In addition to what Deif discovered they're also using CSS transition
transition: all 0.3s ease-out;
and also make use of the "::selection" pseudo class for their "aside" class, see developer.mozilla.org

I need to find prototype.js based slide out tab?

I have founded this -> http://www.building58.com/examples/tabSlideOut.html
But there are some reasons that i dont want to use it:
i need prototype framework instead of jquery
i need an image to open slider (click to open) and when it opened image will change to "click to close"
Maybe someone has already the same solution of my question?
thank for you help!
CSS transitions were made for this sort of thing! For a demonstration of what you're looking for see http://jsfiddle.net/Fw7MQ/ (The 'handle' changes background colour but you could easily make that a background image instead)
The crucial parts of CSS are;
#drawer {
position: relative;
left: -200px;
/* transition is repeated for all supporting browsers */
-webkit-transition: left 0.5s ease-in-out;
-moz-transition: left 0.5s ease-in-out;
-o-transition: left 0.5s ease-in-out;
-ms-transition: left 0.5s ease-in-out;
transition: left 0.5s ease-in-out;
}
#drawer.open {
left: 0;
}
The 'drawer' has a class name added or removed as necessary using this tiny javascript snippet;
Event.observe('handle', 'click', Element.toggleClassName.curry('drawer', 'open'))
...but you could dispense with even that if the animation was done on mouseover instead - change the CSS selector from #drawer.open to #drawer:hover.
For older browsers it degrades gracefully, the animation doesn't play but the drawer still appears in the right place.

Categories