search
HomeWeb Front-endHTML TutorialIssues to note when using css3 animation and jquery animation_html/css_WEB-ITnose

When I was writing a demo a while ago, I encountered a problem. When adding css3 or jquery animation to an element, the css attribute of the element cannot be accurately obtained before the animation ends.

1. css3 animation discussion

Look at the code first:

html:

<div id="hehe"></div><button class="change">change</button><button class="get">get</button>

css:

#hehe {    width: 100px;    height: 100px;    padding: 10px;    background: red;    -webkit-transition: all 10s ease-out;    -moz-transition: all 10s ease-out;    -o-transition: all 10s ease-out;    transition: all 10s ease-out;}

js:

$(document).ready(function() {    $('.change').click(function() {        $('#hehe').width(400);    });    $('.get').click(function() {        console.log($('#hehe').width());    });});

The code is very simple. We added a red background color to the hehe div, and used the animation smoothing effect css3 trasition in css3 (for how to use css3 trasition, please refer to this article: http://www.w3cplus.com/content/css3-transition , written in great detail). That is to say, when hehe changes CSS attributes such as width, height, background or font, there will be an ease-out (slow down) smooth transition effect, which lasts for 10 seconds. For example, if we click the change button, the width of hehe will take 10 seconds and slowly increase to 400px, producing an animation effect.

Here comes the question: When the animation is in progress, I click the get button, what is the width of the hehe output by the console?

At first I thought it was 400, because when I clicked change, I saw "

" when inspecting the element. The answer is no, what we get is the width of hehe at the click moment during the animation.

Let’s modify the console code:

console.log($('#hehe').css('width');

Now we see that during the entire animation process, when we click get, all we get is 400px. This shows that width() in jquery obtains the width of the element in real time, while css('width') simply obtains the css value of the element, because it also has units.

2. jquery animation discussion

jquery animation is easier to understand. We remove the css code of css3 transition and replace it with jquery animate.

$(document).ready(function() {    console.log($('#hehe').width());    $('.change').click(function() {        $('#hehe').animate({width: "400px"}, 10000);    });    $('.get').click(function() {        console.log($('#hehe').width());    });});

Since jquery animation actually dynamically adds styles to elements, whether we use width() or css('width' ) to obtain the width of hehe, which changes dynamically. However, there is one difference. The width obtained by using width has no unit px, and they are all integers; but the width obtained by using css('width') has units, and the number of digits after the decimal point is very long.

Summary:

After discussing so much, the key is to tell everyone that when using animation, if you go to get it immediately Changing css attributes, especially width and height, are prone to problems. You must wait until the animation ends to get the attributes of these elements. Or you can specify the CSS properties you want to animate. For example, if you want to get the width and height in animation, but in fact the animation effect you want is just a change in background color, then you can write like this when writing css3 transition

-webkit-transition: background .7s ease-out;-moz-transition: background .7s ease-out;-o-transition: background .7s ease-out;transition: background .7s ease-out;

In this way, the animation effect will only be displayed on the background color, and the width and height will not change dynamically.

Similarly, for the other two effects in css3 animation, transform (http://www.w3cplus.com/content/css3-transform) and animation (http://www.w3cplus.com/content /css3-animation) also did a test and found that it is the same as transition. During the animation process, the element attributes change dynamically. Even when animation is used, the width obtained by using css('width') also changes. . Interested students can try it.

Therefore, we need to pay more attention to the impact of animation when using css3 animation.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment