js timer to implement simple animation effects
js can implement the timer function. This article mainly shares with you examples of js timer + simple animation effects. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let's follow the editor and take a look. I hope it can help everyone make a better timer using js.
1. Swipe down
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>向下滑动</title> <style> body { margin: 0px; } #show { width: 200px; /* 高度为 0 */ height: 100px; background-color: lightcoral; margin: 0 auto; /* 设置为隐藏 */ /*display: none;*/ } </style> </head> <body> <p id="show"></p> <script> var show = document.getElementById('show'); /*show.style.display = 'block'; var t = setInterval(function(){ var style = window.getComputedStyle(show,null); var height = parseInt(style.height); // 判断当前的高度是否为 400 if (height >= 400){ clearInterval(t); } else { height++; show.style.height = height + 'px'; } },50);*/ slideDown(show,400); /* 将上述实现的向下滑动效果,封装在一个固定的函数中 * 设计当前实现向下滑动效果函数的形参 * elem - 表示实现向下滑动效果的元素 * maxHeight - 表示元素向下滑动的最大高度值 * 函数的逻辑与默认设置CSS样式属性的值无关 */ function slideDown(elem, maxHeight){ // 操作的元素默认的display值为none elem.style.display = 'block'; elem.style.height = '0px'; var t = setInterval(function(){ var style = window.getComputedStyle(elem,null); var height = parseInt(style.height); // 判断当前的高度是否为 400 if (height >= maxHeight){ clearInterval(t); } else { height++; elem.style.height = height + 'px'; } },50); } </script> </body> </html>
2.Move effect
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>移动效果</title> <style> body { margin: 0px; } #box { width: 100px; height: 100px; background-color: lightcoral; position: absolute; left: 100px; top: 100px; } </style> </head> <body> <p id="box"></p> <script> var box = document.getElementById('box'); box.onclick = function(){ clearInterval(t); } /* * 向右移动 * 当前元素移动到页面的最右边时 -> 向左移动 * 向左移动 * 当前元素移动到页面的最左边时 -> 向右移动 */ var flag = false;// 默认表示向右 var speed = 1;// 表示每次变化的值 t = setInterval(function(){ //speed += 0.01; // 获取当前页面的宽度 var WIDTH = window.innerWidth; var style = window.getComputedStyle(box,null); var left = parseInt(style.left); var width = parseInt(style.width); // 判断当前元素移动的方向 if (flag){// 向左移动 left -= speed; } else {// 向右移动 left += speed; } // 判断什么情况下,向左移动;判断什么情况下,向右移动 if ((left + width) >= WIDTH){// 向左移动 flag = true; } else if (left <= 0){// 向右移动 flag = false; } box.style.left = left + 'px'; },10); </script> </body> </html>
3. Combination of events and animation
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>事件与动画结合</title> <style> body { margin: 0px; } </style> </head> <body> <script> // 获取<body>元素 var body = document.body; // 当页面加载完毕后,设置当前<body>元素的高度为当前浏览器窗口的高度 window.onload = function(){ setHeight(body); }; // 当用户改变浏览器窗口的大小时,重新设置<body>元素的高度(等于当前窗口的高度) window.onresize = function(){ setHeight(body); }; // 定义函数 - 设置<body>元素的高度等于当前窗口的高度 function setHeight(elem){ elem.style.height = window.innerHeight + 'px'; } var width = 100,height = 100; // 为<body>元素绑定click事件 body.onclick = function(event){ var x = event.clientX; var y = event.clientY; // 创建<p>元素,显示的位置在鼠标当前的坐标值 var p = document.createElement('p'); p.setAttribute('class','circle'); body.appendChild(p); // rgb(0,0,0)格式 -> 颜色随机 var r = parseInt(Math.random()*255); var g = parseInt(Math.random()*255); var b = parseInt(Math.random()*255); p.style.width = width + 'px'; p.style.height = height + 'px'; p.style.backgroundColor = 'rgb('+r+','+g+','+b+')'; p.style.borderRadius = '50%'; p.style.opacity = 1; p.style.position = 'absolute'; p.style.left = x - width/2 + 'px'; p.style.top = y - height/2 + 'px'; animate(p); } // 定义函数 -> 实现动画效果 function animate(elem){ var style = window.getComputedStyle(elem,null); /*var width = parseInt(style.width); var height = parseInt(style.height); var left = parseInt(style.left); var top = parseInt(style.top); width++; height++; elem.style.width = width + 'px'; elem.style.height = height + 'px'; elem.style.left = (left - 0.5) + 'px'; elem.style.top = (top - 0.5) +'px';*/ var opacity = style.opacity; if (opacity <= 0){ clearTimeout(t); // 删除当前元素 } opacity -= 0.01; elem.style.opacity = opacity; // 设定定时器 var t = setTimeout(function(){ animate(elem); },50); } </script> </body> </html>
Related recommendations:
Detailed explanation of setting and clearing timers in JavaScript
About the analysis of the principles of timers in JavaScript
Detailed explanation of how to create and use advanced timer instance code in javascript
The above is the detailed content of js timer to implement simple animation effects. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



You may have encountered the problem of green lines appearing on the screen of your smartphone. Even if you have never seen it, you must have seen related pictures on the Internet. So, have you ever encountered a situation where the smart watch screen turns white? On April 2, CNMO learned from foreign media that a Reddit user shared a picture on the social platform, showing the screen of the Samsung Watch series smart watches turning white. The user wrote: "I was charging when I left, and when I came back, it was like this. I tried to restart, but the screen was still like this during the restart process." Samsung Watch smart watch screen turned white. The Reddit user did not specify the smart watch. Specific model. However, judging from the picture, it should be Samsung Watch5. Previously, another Reddit user also reported

Speaking of ASSASSIN, I believe players will definitely think of the master assassins in "Assassin's Creed". They are not only skilled, but also have the creed of "devoting themselves to the darkness and serving the light". The ASSASSIN series of flagship air-cooled radiators from the appliance brand DeepCool coincide with each other. Recently, the latest product of this series, ASSASSIN4S, has been launched. "Assassin in Suit, Advanced" brings a new air-cooling experience to advanced players. The appearance is full of details. The Assassin 4S radiator adopts a double tower structure + a single fan built-in design. The outside is covered with a cube-shaped fairing, which has a strong overall sense. It is available in white and black colors to meet different colors. Tie

With the arrival of spring, everything revives and everything is full of vitality and vitality. In this beautiful season, how to add a touch of color to your home life? Haqu H2 projector, with its exquisite design and super cost-effectiveness, has become an indispensable beauty in this spring. This H2 projector is compact yet stylish. Whether placed on the TV cabinet in the living room or next to the bedside table in the bedroom, it can become a beautiful landscape. Its body is made of milky white matte texture. This design not only makes the projector look more advanced, but also increases the comfort of the touch. The beige leather-like material adds a touch of warmth and elegance to the overall appearance. This combination of colors and materials not only conforms to the aesthetic trend of modern homes, but also can be integrated into

With its compact size, the ITX platform has attracted many players who pursue the ultimate and unique beauty. With the improvement of manufacturing processes and technological advancements, both Intel's 14th generation Core and RTX40 series graphics cards can exert their strength on the ITX platform, and gamers also There are higher requirements for SFX power supply. Game enthusiast Huntkey has launched a new MX series power supply. In the ITX platform that meets high-performance requirements, the MX750P full-module power supply has a rated power of up to 750W and has passed 80PLUS platinum level certification. Below we bring the evaluation of this power supply. Huntkey MX750P full-module power supply adopts a simple and fashionable design concept. There are two black and white models for players to choose from. Both use matte surface treatment and have a good texture with silver gray and red fonts.

A large model that can automatically analyze the content of PDFs, web pages, posters, and Excel charts is not too convenient for workers. The InternLM-XComposer2-4KHD (abbreviated as IXC2-4KHD) model proposed by Shanghai AILab, the Chinese University of Hong Kong and other research institutions makes this a reality. Compared with other multi-modal large models that have a resolution limit of no more than 1500x1500, this work increases the maximum input image of multi-modal large models to more than 4K (3840x1600) resolution, and supports any aspect ratio and 336 pixels to 4K Dynamic resolution changes. Three days after its release, the model topped the HuggingFace visual question answering model popularity list. Easy to handle

In the current era of rapid technological development, laptops have become an indispensable and important tool in people's daily life and work. For those players who have high performance requirements, a laptop with powerful configuration and excellent performance can meet their hard-core needs. With its excellent performance and stunning design, the Colorful Hidden Star P15 notebook computer has become the leader of the future and can be called a model of hard-core notebooks. Colorful Hidden Star P1524 is equipped with a 13th generation Intel Core i7 processor and RTX4060Laptop GPU. It adopts a more fashionable spaceship design style and has excellent performance in details. Let us first take a look at the features of this notebook. Supreme equipped with Intel Core i7-13620H processing

In today's smartphone market, screen quality has become one of the key indicators to measure the overall performance of a mobile phone. iQOO's Neo series has always been committed to providing users with excellent gaming experience and visual enjoyment. The latest product iQOO Neo9SPro+ uses a "Three Good Eye Protection Gaming Screen". Next, let's take a look at the quality of this screen. How brilliant. iQOO Neo9S Pro+ is equipped with a 1.5 KOLED e-sports direct screen, which supports flagship LTPO adaptive refresh rate from 1Hz to 144Hz, which means that it can achieve ultra-low power standby state when displaying static content, and it can also be intelligent during gaming. Switch to dynamic high from 90Hz to 144Hz

Many photography enthusiasts like to use lenses. Their shooting needs are very changeable, so when it comes to lens selection, they prefer a more versatile product, which is what we commonly call "one lens to conquer the world" lens. It just so happens that Nikon has launched a new product, the NIKKOR Z28-400mmf/4-8VR lens, a true "one lens that can conquer the world" lens. The lens covers from the 28mm wide-angle end to the 400mm telephoto end. Equipped with its Z-mount camera, it can easily shoot a very rich range of photography themes and bring about a rich change of perspective. Today, we will talk to you about this NIKKOR Z28-400mmf/4-8VR lens through our recent use experience. NIKKOR Z28-400mmf/4-8VR is
