简体   繁体   English

如何使用具有过渡缓入效果的JavaScript隐藏/显示div?

[英]How to hide/show the div using javascript with transition ease-in-out effect?

This is my code which hide/show the div...but I want transition effect while hide/show in javascript like ease-in-out. 这是我的代码,它隐藏/显示div ...,但是我想在转入/隐藏等javascript中隐藏/显示时具有过渡效果。 How to achieve this in javascript? 如何在javascript中实现呢?

function showHide(shID) {

    if (document.getElementById(shID)) {
        if (document.getElementById(shID + '-show').style.display != 'none') {
            document.getElementById(shID + '-show').style.display = 'none';
                document.getElementById(shID).style.display = 'block';
                window.scrollTo(0, 2346);
            }
        else {

            document.getElementById(shID + '-show').style.display = 'inline';
            document.getElementById(shID).style.display = 'none';

        }
    }
}

As has been pointed out, you should use CSS3 transitions if you want to do this in a standard way which will also not cause a lot of trouble because of high maintenance. 正如已经指出的那样,如果要以标准方式执行此操作,则应使用CSS3过渡,这也不会因维护成本高而引起很多麻烦。

However, the problem then is you're trying to use a transition on a property with no quantifiable transitional values. 但是,问题是,您正在尝试对没有可量化过渡值的属性使用过渡。 There is no middle ground between display: block; display: block;之间没有中间立场display: block; and display: none; display: none; so you must use something which does have transitional values. 因此您必须使用具有过渡值的东西。 Usually a property such as width or height is appropriate, but you can also use it on opacity , left , top , etc. (so basically any property with a numeric value). 通常,诸如widthheight类的属性是适当的,但您也可以在opacitylefttop等上使用它(因此,基本上任何具有数字值的属性)。

This CSS is for an element which fades and slides in and out of a document from the top. 该CSS适用于从顶部淡入淡出和滑入文档的元素。

.panel {
    position: absolute;
    top: 0px;
    opacity: 0;
    -moz-transition: opacity .25s ease-in-out , top .25s ease-in-out;
    -o-transition: opacity .25s ease-in-out , top .25s ease-in-out;
    -webkit-transition: opacity .25s ease-in-out , top .25s ease-in-out;
    transition: opacity .25s ease-in-out , top .25s ease-in-out;
}
.panel.show {
    top: 50px;
    opacity: 100;
}

Here's a Javascript that goes through the elements, checks for a data-toggle attribute, and assigns a click handler if it finds it. 这是一个Javascript,它遍历所有元素,检查data-toggle属性,并在找到它时分配一个click处理程序。 The handler toggles the show class for the targeted element. 处理程序为目标元素切换show类。

function toggleElem(evt) {
    var cn = 'show';
    var panel = document.getElementById(evt.target.getAttribute('data-toggle'));
    var classes = panel.className.split(/\s+/);
    var x = classes.indexOf(cn);

    if(x >= 0)
        { classes.splice(x,1); }
    else
        { classes.push(cn); }

    panel.className = classes.join(' ');
}

var elems = document.body.getElementsByTagName('*');

for(var i=0,l=elems.length;i<l;i++) {
    var elem = elems[i];
    var toggle = elem.getAttribute('data-toggle');

    if(toggle !== null)
        { elem.addEventListener('click',toggleElem,false); }
}

Example HTML. HTML示例。

<input type="button" data-toggle="foo" value="Toggle"/>
<div id="foo" class="panel">
    <p>This is a transitioned panel.</p>
</div>

Because of the fact you would be using opacity instead of display you may also run into the snag that the element you want to toggle, though invisible, will still be there as far as your cursor is concerned. 由于您实际上将使用opacity而不是display您可能还会遇到这样的麻烦:要切换的元素虽然不可见,但就光标而言仍将存在。 You may then want to use setTimeout() to change the display value in addition to performing the class change to keep your transitioned element from catching input when hidden. 然后,除了执行类更改之外,您可能还希望使用setTimeout()来更改display值,以防止过渡元素在隐藏时捕获输入。

I am not sure, why you are not using CSS3 transitions. 我不确定,为什么不使用CSS3过渡。

But to keep it all strictly javascript probably this source code is helpful: https://github.com/jquery/jquery/blob/master/src/effects.js#L107 但是严格地使用javascript可能会使用以下源代码: https//github.com/jquery/jquery/blob/master/src/effects.js#L107

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM