CSS3 use JavaScript to trigger transition effects (transition)

Source: Internet
Author: User

Using: hover and: A pseudo class such as focus, we can easily switch elements from one style to another, and switching can have a transition effect. But sometimes we want to use JS to drive the transition (that is, to trigger the transition in code) is also achievable.

As with the normal transition, create two style rules first, one is the initial state of the element, one is the state of the end of the transition. Then use JS in the appropriate Hing to modify the corresponding elements of the style can be.

Here's a sample demo with a day and night view Picture:

(1) The day and night scene are two pictures stacked together. The initial transparency of the night view is 0, covering the top of the day map.

(2) Click "See Night View" button, night view chart transparency gradually from 0 to 1, it seems that the screen slowly become night.

(3) Click on "See the Day" button, night view of the transparency gradually from 1 transition to 0, it looks like the restoration of own scene.

The online sample is as follows:

look at the night view See the Day view



<!doctype html>
<title>hangge.com</title>

<style>
IMG {
Position:absolute;
Transition:opacity 5s;
-webkit-transition:opacity 5s;
}

. Solid {
Opacity:1;
}

. Transparent {
opacity:0;
}
</style>

<script>
function tonight () {
var nightimage = document.getElementById ("Nightimage");
Nightimage.classname = "SOLID";
}

function today () {
var nightimage = document.getElementById ("Nightimage");
Nightimage.classname = "Transparent";
}
</script>
<body>
<button onclick= "Tonight ()" > See Night View </button>
<button onclick= "Today ()" > See Day View </button>
<div>


</div>
</body>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.