Replace jQuery slide with animate() CSS3

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 20:37:22

问题


I have jQuery slideUp and down and on a page and the performance of animations is very bad. So i want to replace the sliding function with .animate() or .css() to utilize CSS3 animations (which are generally smoother than jQuery)

here is my code

jQuery('.close').on("click", function() {
var parent = jQuery(this).parent('.parentbox');
parent.children('.box').slideUp('500');
jQuery(this).hide();
parent.children('.open').show();
parent.animate({"opacity":"0.4"});
});

jQuery('.open').on("click", function() {
var parent = jQuery(this).parent('.parentbox');
parent.children('.box').slideDown('500');
jQuery(this).hide();
parent.children('.close').show();
parent.animate({"opacity":"1"});
});

Now if i replace .slideUp with .animate({"height":"0px"}); how can i revert it back to the previous height when .open is clicked?

I use cookies to close the box if it was closed the last time. This leaves me unable to use .height() to check box's height as in some cases it might be closed.


回答1:


You can use .animate({ "height":"hide" }) (and the reverse) to accomplish this.

Example:

function slideUp() {
    $(".slideme").animate({ "height": "hide" });
}

function slideDown() {
    $(".slideme").animate({ "height": "show" });  
}

jsFiddle: http://jsfiddle.net/8VVde/14/




回答2:


I think that perhaps it just isn't worth it. I'm not sure a full CSS3 replacement will get much more succinct than this mess. And this is assuming you're using jQuery Transit. The trick is that you need the rendered height before you start the CSS3 transition, and that isn't trivial to get.

var $new = $(content)
    .css({'visibility': 'hidden', 'position': 'absolute', 'overflow': 'hidden'})
    .appendTo($append);
var height = $new.height();
$new.css({'height': 0})
    .css({'position': 'static', 'visibility': 'visible'})
    .transition({'height': height + 'px'});



回答3:


I am trying to replace jQuery slideDown.

The suggestion by Anders Holmström works like a charm! Can animate be replaced by "transition()" (http://ricostacruz.com/jquery.transit) though?

This does not seem to animate, but is instead finished when clicked:

el.transition({ "display": "block", "height": "show"}, 250);


来源:https://stackoverflow.com/questions/12564735/replace-jquery-slide-with-animate-css3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!