jQuery slideToggle just shows and hides but doesn't animate correctly

烈酒焚心 提交于 2019-12-11 04:39:06

问题


In this example my div container just hides and shows but doesn't do the awesome jQuery animation.

$('.button').click(function() {
    $('.class1').slideToggle();
});

Can anybody say me why? I know that animations on tables don't work correctly but on divs it should be okay.

UPDATE Thanks for all the answers. The information I was missing is that position absolute removes the element from the DOM. And yeah I know I should put the style in a css--this version was just for quick testing, but thanks for the answers!!!


回答1:


.slideToggle() animates the height of an element from 0% - 100%, hiding its children as it goes.

By using position:absolute throughout your fiddle, you are removing the child elements from the flow of the DOM and interfering with the ability of the parent element to incrementally hide its children.

If you set overflow to hidden, and remove position:absolute from the children, this works properly.

http://jsfiddle.net/7xzMa/6/




回答2:


UPDATED YOUR FIDDLE - http://jsfiddle.net/7xzMa/14/ --- cleaned the divs and you should place css into a style sheet so its easier to edit. "display" should be set to "none". unless you would like it to be visible then click to hide. http://jsfiddle.net/7xzMa/8/




回答3:


Artur, my recommendation is to restructure your code entirely, there is way too much going on. Short and simple is always best. I'd start by getting rid of the "style" attributes in your html. Make sure to put styles in CSS (it's cleaner and easier to maintain -- much better than deleting styles from multiple tags).

I just made you a demo, hope it helps! Feel free to use it! Happy coding! http://jsfiddle.net/7xzMa/17/




回答4:


use JQuery UI

$(document).ready(function() {
    $('.button').click(function() {
        $('.class1').toggle('slide',{direction: 'up'},500);
    });
});


来源:https://stackoverflow.com/questions/11836751/jquery-slidetoggle-just-shows-and-hides-but-doesnt-animate-correctly

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