简体   繁体   中英

Javascript transition duration won't work

I wan't to make show() and hide() method like jquery have, but with pure javascript because I want to modify how the element show and hide. But after my attempts, I've changed where the code placed, changed the code, etc, still it won't work. Only for a few times it was work but it was inconsistent (when I try to run it through firefox, it work for once but never again). The display (block and none) and the exact width and height is work, but not the 2s transition. How to fix this?

<!doctype html>
<html>

<head>
<style>
    div {
        width: 0px;
        height: 0px;
        background-color: yellow;
        border: 1px solid black;
        display: none;
    }
</style>
</head>

<body>

<div>
</div>

<button>
    click !
</button>

<script>
    var x = document.getElementsByTagName("button");
    var y = document.getElementsByTagName("div");

    x[0].onclick = fungsi;

    function fungsi() {
        if (y[0].style.display != "block") {
            y[0].style.display = "block";
            y[0].style.transition = "width 2s";
            y[0].style.transition = "height 2s";
            y[0].style.width = "100px";
            y[0].style.height = "100px";
        } else {
            y[0].style.display = "";
            y[0].style.transition = "width 2s";
            y[0].style.transition = "height 2s";
            y[0].style.width = "";
            y[0].style.height = "";
        }
    };
</script>
</body>

</html> 

It could be something to do with vendor prefixes

So as well as having the following:

y[0].style.transition

You will also need:

y[0].style.mozTransition
y[0].style.webkitTransition

Try that, hopefully should work.

What you are doing is not animating show and hide with "pure javascript", it really is animating show and hide with CSS3 . You are just setting the CSS properties trough javascript!

That being said, CSS3 transitions are not supported by every browser . For example even IE9 does not support it. Some other browser only work with prefixed versions of this property .

Try setting -moz-transition , -webkit-transition and -o-transition too.

For more details see: Transition browser support

However if you expect your animation to work across all major platforms I suggest you to use jQuery and try adjusting the settings to your desired behavior..

I've messed around with your code and found that the reason for your problem was the switching from display: none; to display: block; and back. I've made a simple solution for this if you would like to use it.

Here is the modified CSS code.

div {
    width: 0px;
    height: 0px;
    background-color: yellow;
    border: none;
}

Here is the modified JS code.

var x = document.getElementsByTagName("button");
var y = document.getElementsByTagName("div");
x[0].onclick = fungsi;
var expanded = false;

function fungsi() {
    y[0].style.transition = "all 2s";
    if (!expanded) {
        y[0].style.border = "1px solid black";
        y[0].style.width = "100px";
        y[0].style.height = "100px";
        expanded = true;
    } else {
        var applyBorder = function () {
            y[0].style.border = "none";
        };
        y[0].style.width = "0";
        y[0].style.height = "0";
        expanded = false;
        setTimeout(applyBorder, 2000);
    }
};

And here is a JSFiddle of this code for an example.

When you run

y[0].style.transition = "width 2s";
y[0].style.transition = "height 2s";

The first line will be overwritten by the second. So the transition goes only for width or height at a time, and when one of them is 0, the transition will be invisible.

You should set transition like this:

y[0].style.transition = 'width 2s,height 2s';

Or just set it for all properties that support transition:

y[0].style.transition = 'all 2s';

BTW, since the transition property is not changing, you should set them outside the changing part.

Another problem is, the <div> must be visible before the animation starts, otherwise it will have the desired width and height once become visible, and no transition is needed any more. visibility is another choice since an element with visibility: hidden still takes the place.

Here is a working copy of code:

<!doctype html>
<html>

<head>
<style>
    div {
        width: 0px;
        height: 0px;
        background-color: yellow;
        border: 1px solid black;
        /* Use visibility instead of display */
        visibility: hidden;
    }
</style>
</head>
<body>

<div>
</div>

<button>
    click !
</button>

<script>
    var x = document.getElementsByTagName("button");
    var y = document.getElementsByTagName("div");

    x[0].onclick = fungsi;
    // set transition
    y[0].style.transition = "width 2s,height 2s";

    function fungsi() {
        if (y[0].style.visibility != "visible") {
            y[0].style.visibility = "visible";
            y[0].style.width = "100px";
            y[0].style.height = "100px";
        } else {
            y[0].style.visibility = "";
            y[0].style.width = "";
            y[0].style.height = "";
        }
    };
</script>
</body>

</html>

And something more, if you want to hide the element after the animation ends, setTimeout will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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