這篇文章為大家帶來了關於javascript的相關知識,其中主要介紹了操作dom元素添加動畫效果的相關問題,下面一起來看一下,希望對大家有幫助。
【相關推薦:javascript影片教學、web前端】
##DOM動畫效果- 讓一個元素從左到右進行運動
-
<div></div>
var box = document.getElementById("box"); var t = null; t = setInterval(function(){ })
t = setInterval(function(){終止條件})
// 元素的属性值 === 目标点 if(dom.attr === target){ clearInterval(t); }運動的三要素
- 起始點
一個動作的起始點其實就是目前元素的位置,我們透過API取得目前元素的位置,讓這個位置作為運動的起始。
- 目標
- 速度
- 讓元素透過計時器在很短的間隔內進行CSS屬性值的改變
- 這樣連續的運動在使用者看來就是動畫效果
封裝的主要作用是讓元素可以在短時間間隔內不斷改變屬性實現動畫效果
單屬性運動框架:
<button>开始运动</button> <div></div> <div></div> <script> var box = document.getElementById("box"); var btn = document.getElementById("btn"); var target = 500; // 速度可以根据 起始点和目标点进行判断,从而决定正负; var speed = 17; // - 方向; btn.onclick = function(){ // 1. 获取元素初始位置; var _left = box.offsetLeft; speed = target - _left >= 0 ? speed : -speed ; var interval = setInterval( function(){ // 4. 判定运动的终止条件; if(Math.abs(target - _left) <= Math.abs(speed) ){ // 因为终止时有可能没有到达目标点,因此我们把元素赋值到目标点位置; box.style.left = target + "px"; clearInterval( interval ); }else{ // 2. 元素根据初始位置进行改变; _left += speed; // 3. DOM操作,根据已有数据让元素属性发生改变; box.style.left = _left + "px"; } } , 30) } </script>
- 勻速運動封裝
<script>// - 提取属性名作为参数; btn.onclick = function(){ animate( "left", 500 )}function animate( attr , target , speed = 10 ){ // 1. 获取元素初始样式 var _style = getComputedStyle( box ); // 2. 根据属性要求取出当前的属性的属性值; var _css_style = parseInt(_style[attr]); speed = target - _css_style >= 0 ? speed : -speed ; var interval = setInterval( function(){ // 4. 判定运动的终止条件; if(Math.abs(target - _css_style) <= Math.abs(speed) ){ // 因为终止时有可能没有到达目标点,因此我们把元素赋值到目标点位置; box.style[attr] = target + "px"; clearInterval( interval ); }else{ // 2. 元素根据初始位置进行改变; _css_style += speed; // 3. DOM操作,根据已有数据让元素属性发生改变; box.style[attr] = _css_style + "px"; } } , 30)}</script>
- 相容透明度
<script>function animate( dom , attr , target , speed = 10 ){ // 1. 获取元素初始样式 var _style = getComputedStyle( dom ); // 2. 根据属性要求取出当前的属性的属性值; if( attr === "opacity"){ var _css_style = parseInt(_style[attr] * 100 ); target *= 100; }else{ var _css_style = parseInt(_style[attr]); } speed = target - _css_style >= 0 ? speed : -speed ; var interval = setInterval( function(){ // 4. 判定运动的终止条件; if(Math.abs(target - _css_style) <= Math.abs(speed) ){ // 因为终止时有可能没有到达目标点,因此我们把元素赋值到目标点位置; if( attr === "opacity"){ dom.style[attr] = target / 100; }else{ dom.style[attr] = target + "px"; } clearInterval( interval ); }else{ // 2. 元素根据初始位置进行改变; _css_style += speed; // 3. DOM操作,根据已有数据让元素属性发生改变; if( attr === "opacity"){ dom.style[attr] = _css_style / 100 ; }else{ dom.style[attr] = _css_style + "px"; } } } , 30)}</script>
- 緩衝運動
- 緩衝運動是一種運動方式
- 這種運動方式是速度在運動過程中會有改變的運動
- 距離越小速度越小
<button>开始运动</button> <div></div> <script> var box = document.getElementById("box"); var btn = document.getElementById("btn"); btn.onclick = function(){ animate( box , "left" , 500 ) } function animate( dom , attr , target , transition = "buffer", speed = 10 ){ var _style = getComputedStyle( dom ); if( attr === "opacity"){ var _css_style = parseInt(_style[attr] * 100 ); target *= 100; }else{ var _css_style = parseInt(_style[attr]); } if( transition === "liner"){ speed = target - _css_style >= 0 ? speed : -speed ; } var interval = setInterval( function(){ if( transition === "buffer"){ // 计算速度; speed = (target - _css_style) / 10; //速度不取整在小数部分会做很多无意义的计算; speed = speed > 0 ? Math.ceil(speed) :Math.floor( speed ) } if(Math.abs(target - _css_style) <= Math.abs(speed) ){ // 因为终止时有可能没有到达目标点,因此我们把元素赋值到目标点位置; if( attr === "opacity"){ dom.style[attr] = target / 100; }else{ dom.style[attr] = target + "px"; } clearInterval( interval ); }else{ // 2. 元素根据初始位置进行改变; _css_style += speed; // 3. DOM操作,根据已有数据让元素属性发生改变; if( attr === "opacity"){ dom.style[attr] = _css_style / 100 ; }else{ dom.style[attr] = _css_style + "px"; } } } , 30) } </script> //只需要改变里面transition的值就可以调整运动模式 //buffer为缓冲运动 //liner为匀速运动多元屬性運動框架(拓展)
- 多屬性運動框架
- 我們在多次呼叫animate的時候會開啟多個計時器
- 因為定時器之中的資料都一樣,我們看不出在效果上的差異
- 但是多次開啟定時器會極其嚴重的消耗計算機性能
- 開啟當前定時器之前關閉上一個定時器
<script>// 在一个定时器之中,用for循环同时执行多次dom样式操作; // 1. 需要优化的部分:参数,要把样式部分的参数优化成一个对象; function animate( dom , attrs , transition = "buffer", speed = 10 ){ var _style = getComputedStyle( dom ); // 获取元素当前的属性 : for(var attr in attrs ){ // attr ? 要过渡的css属性名; // attrs[attr] ? 要过渡的当前属性; attrs[attr] = { target : attrs[attr], // 元素当前的属性放入到这个对象之中; now : parseInt(_style[attr]) } } // 因为直接关闭interval是没有作用的,此时的inteval是一个局部变量,每次animate被调用的时候都会直接重置; // 我们应该吧定时器的id放在当前正在执行过渡效果的dom对象上; clearInterval(dom.interval); dom.interval = setInterval( function(){ for(var attr in attrs){ // 取出 attrs 之中的目标点和当前值; speed = (attrs[attr].target - attrs[attr].now) / 10 ; // 根据速度正负,进行速度取整; speed = speed > 0 ? Math.ceil( speed ) : Math.floor( speed ); // 判定终止条件; if( attrs[attr].target === attrs[attr].now){ // 删除已经到达目标点的属性; delete attrs[attr] // 判定attrs里面已经没有属性了; for(var a in attrs){ return false; } clearInterval(dom.interval); }else{ attrs[attr].now += speed; dom.style[ attr ] = attrs[attr].now + "px"; } } } , 30)}// 优化参数之后,key值是等待运动的css属性,value值是元素的目标; btn.onclick = function(){ animate( box , { width : 500 , height : 400 } )}</script>輪播圖功能實作
<style>
*{
margin: 0;
padding: 0;
}
.container{
width: 1130px;
height: 286px;
margin: 0 auto;
position: relative;
overflow: hidden;
}
.wrapper{
width: 6780px;
position: absolute;
left: 0;
}
.slide{
float: left;
}
.slide , .slide img{
width: 1130px;
height: 286px;
}
.button-prev{
left: 0;
background-position: 30px center;
background-image: url(https://static.zcool.cn/git_z/z/widget/slider/images/svg/left_arrow.svg?v=2);
top: 0;
width: 13px;
height: 100%;
padding: 0 80px;
border-radius: 2px;
position: absolute;
background-repeat: no-repeat;
}
.button-prev:hover{
background-image: url(https://static.zcool.cn/git_z/z/widget/slider/images/svg/left_arrowhover.svg?v=2);
}
.button-next{
right: 0;
background-position: 91px center;
background-image: url(https://static.zcool.cn/git_z/z/widget/slider/images/svg/right_arrow.svg?v=2);
top: 0;
width: 13px;
height: 100%;
padding: 0 80px;
border-radius: 2px;
position: absolute;
background-repeat: no-repeat;
}
.button-next:hover{
background-image: url(https://static.zcool.cn/git_z/z/widget/slider/images/svg/right_arrowhover.svg?v=2);
}
.pagination{
position: absolute;
bottom: 10%;
width: 100%;
height: 10px;
left: 30px;
}
.pagination span{
display: inline-block;
width: 10px;
height: 10px;
margin-left: 10px;
border-radius: 50%;
background-color: cornflowerblue;
border: 2px solid transparent;
background-clip: content-box;
}
.pagination span.active{
border: 2px solid skyblue;
box-shadow: 0 0 5px skyblue;
background-color: #fff;
}
</style>
<div></div>
<div></div>
<script></script>
<script>
// 轮播图的核心就是左右切换按钮,实现显示元素下标的改变;
var index = 0 ;
var prev = 0;
var next_btn = document.querySelector(".button-next");
var prev_btn = document.querySelector(".button-prev");
var slides = document.querySelectorAll(".slide");
var wrapper = document.querySelector(".wrapper");
// 自动播放的阻止功能是在鼠标移入container容器之中就触发的;
var container = document.querySelector(".container");
var bullets = document.querySelectorAll(".pagination span");
// 绑定事件
// - 轮播图改变下标功能必须设置边界;
function bindEvent(){
next_btn.onclick = function(){
add();
bannerAnimate();
}
prev_btn.onclick = function(){
reduce()
bannerAnimate();
}
container.onmouseover = function(){
stop();
}
container.onmouseout = function(){
autoPlay();
}
bullets.forEach( function( ele , i ){
ele.onmouseover = function(){
prev = index;
// 防止穿帮逻辑;
// - 如果在假的第0张图片上(在最后一张图片上)
// - 我们先让真假图片呼喊然后在进行元素的动画效果;
if( index === 5 ){
wrapper.style.left = 0;
}
index = i;
bannerAnimate();
}
})
}
// 下标增加;
function add(){
prev = index;
if( index === slides.length - 1 ){
// 这个逻辑会在最后一张图片进行切换时进入;
// 我们让wrapper直接位移到开头,改变元素位置的同时让用户无法感知;
wrapper.style.left = 0;
// 我们需要从第0个图片切换到第一个图片;
// -因为我们最后一张图片的显示和开头图片的显示是一样的
index = 1;
}else{
index ++;
}
}
// 下标减少;
function reduce(){
prev = index;
if( index === 0 ){
wrapper.style.left = -(slides.length - 1) * 1130 + "px";
index = slides.length - 2;
}else{
index --;
}
}
// 根据我们的算法去实现动画效果;
function bannerAnimate(){
animate( wrapper , { left : - index * 1130 });
// 给对应的分页器按钮添加active;
// 先去清空所有的类名;
bullets.forEach( function( ele ){
ele.classList.remove("active")
})
// 下标需要进行特殊处理,在显示最后一张图片的时候,给第0个按钮添加active;
bullets[ index === 5 ? 0 : index ].classList.add("active");
}
bindEvent();
var interval = null;
function autoPlay(){
// 间隔3s,让js点击一下下一页按钮;
interval = setInterval( function(){
// 虚拟点击 :
next_btn.dispatchEvent( new Event("click"));
} , 3000 )
}
function stop(){
clearInterval( interval )
}
autoPlay();
</script>






##範例如下
項目背景:uniapp h5應用,為了提示使用者下載,這裡有個提示下載的dom, 本來想來想在每個tabbar 中新增(一共加入四個);但想嘗試換種玩法如下: 效果如下:
直接上程式碼:
function showDownloadTisp() { console.log('--------------------------->showDownloadTisp') // #ifdef H5 const parent = document.querySelector('.uni-tabbar') console.log('parent:', parent) const tips = document.createElement('p') tips.id = 'downloadTisp' tips.setAttribute('style', 'background: rgba(51,51,51,0.75);' ) tips.setAttribute('style', `background: rgba(51,51,51,0.75); position: fixed;width: 100%;height: ${uni.upx2px(120)}px; bottom:${uni.upx2px(-140)}px; display: flex;flex-direction: row;justify-content: space-between;align-items:center;transition:0.5s;` ) const desParent = document.createElement('p') const des = document.createTextNode('Download our App, you will get a better experience.') desParent.appendChild(des) desParent.setAttribute('style', `padding: 0;color: #FFFFFF;font-size:${uni.upx2px(24)}px;margin-left: ${uni.upx2px(27)}px;padding-right: ${uni.upx2px(25)}px;` ) const iosImage = document.createElement('img') iosImage.src = '../static/guide/download ios.png' iosImage.setAttribute('style', `width: ${uni.upx2px(154)}px;height: ${uni.upx2px(54)}px;margin-right: ${uni.upx2px(25)}px;`) const anroidImage = document.createElement('img') anroidImage.src = '../static/guide/download android.png' anroidImage.setAttribute('style', `width: ${uni.upx2px(154)}px;height: ${uni.upx2px(54)}px;margin-right: ${uni.upx2px(50)}px;`) const closedImage = document.createElement('img') closedImage.src = '../static/guide/download closd.png' closedImage.setAttribute('style', `width: ${uni.upx2px(30)}px;height: ${uni.upx2px(30)}px;position: absolute;right:${uni.upx2px(15)}px;top: ${uni.upx2px(15)}px;padding: ${uni.upx2px(5)};` ) tips.appendChild(desParent) tips.appendChild(iosImage) tips.appendChild(anroidImage) tips.appendChild(closedImage) iosImage.onclick = () => { //console.log("iosImage.onclick") window.location.href = 'https://apps.apple.com/cn/app/gbm001/id1574324240' // window.open('https://www.baidu.com/') } anroidImage.onclick = () => { //console.log("anroidImage.onclick") window.location.href = 'https://play.google.com/store/apps/details?id=com.vandream.gbmpro' // window.open('https://www.sina.com.cn/') } closedImage.onclick = () => { console.log("closedImage.onclick") tips.remove() } parent.parentNode.appendChild(tips) //parent.appendChild(tips) setTimeout(() => { tips.style.transform = `translateY(${uni.upx2px(-140) - 50}px);` //console.log(" tips.style.transform done") }, 2500) // #endif }透過程式碼建立節點且程式碼這是style;以及動畫;在應用程式啟動的時候呼叫就可以了;
關於js 設定變換動畫;這邊改成了3D 的形式
##
function showDownloadTisp() { // #ifdef H5 const parent = document.querySelector('.uni-tabbar') // console.log('parent:', parent) const tips = document.createElement('p') tips.id = 'downloadTisp' // tips.setAttribute('style', // 'background: rgba(51,51,51,0.75);' // ) // tips.setAttribute('style', // `background: rgba(51,51,51,0.75); position: fixed;width: 100%;height: ${uni.upx2px(120)}px; bottom:${uni.upx2px(-140)}px; display: flex;flex-direction: row;justify-content: space-between;align-items:center;transition:0.5s;` // ) tips.setAttribute('style', `background: rgba(51,51,51,0.75); position: fixed;width: 100%;height: ${uni.upx2px(120)}px; bottom:50px; display: flex;flex-direction: row;justify-content: space-between;align-items:center;transition:0.5s; transform-origin:center bottom; transform:perspective(900px) rotateX(90deg);` ) const desParent = document.createElement('p') const des = document.createTextNode('Download our App, you will get a better experience.') desParent.appendChild(des) desParent.setAttribute('style', `padding: 0;color: #FFFFFF;font-size:${uni.upx2px(24)}px;margin-left: ${uni.upx2px(27)}px;padding-right: ${uni.upx2px(10)}px;` ) const iosImage = document.createElement('img') // iosImage.src = '../static/guide/download ios.png' iosImage.src = 'https://img.vandream.com/54/0330f6211506cc.png' iosImage.setAttribute('style', `width: ${uni.upx2px(154)}px;height: ${uni.upx2px(54)}px;margin-right: ${uni.upx2px(25)}px;`) const anroidImage = document.createElement('img') // anroidImage.src = '../static/guide/download android.png' anroidImage.src = 'https://img.vandream.com/53/0330f45097465d.png' anroidImage.setAttribute('style', `width: ${uni.upx2px(154)}px;height: ${uni.upx2px(54)}px;margin-right: ${uni.upx2px(50)}px;`) const closedImage = document.createElement('img') // closedImage.src = '../static/guide/download closd.png' closedImage.src = 'https://img.vandream.com/52/0330f523d7709d.png' closedImage.setAttribute('style', `width: ${uni.upx2px(30)}px;height: ${uni.upx2px(30)}px;position: absolute;right:${uni.upx2px(15)}px;top: ${uni.upx2px(15)}px;padding: ${uni.upx2px(5)};` ) tips.appendChild(desParent) tips.appendChild(iosImage) tips.appendChild(anroidImage) tips.appendChild(closedImage) iosImage.onclick = () => { console.log('iosImage.onclick') window.location.href = 'https://apps.apple.com/cn/app/gbm001/id1574324240' // window.open('https://www.baidu.com/') } anroidImage.onclick = () => { console.log('anroidImage.onclick') window.location.href = 'https://play.google.com/store/apps/details?id=com.vandream.gbmpro' // window.open('https://www.sina.com.cn/') } closedImage.onclick = () => { console.log('closedImage.onclick') tips.remove() } parent.parentNode.appendChild(tips) // parent.appendChild(tips) setTimeout(() => { // tips.style.transform = `translateY(${uni.upx2px(-140) - 50}px);` tips.style.transform = 'rotateX(0deg)' // console.log(" tips.style.transform done"):rotateX(90deg); }, 2500) // #endif }
【相關推薦:javascript影片教學
web前端】
以上是實例詳解之操作單一dom元素新增動畫的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python更適合初學者,學習曲線平緩,語法簡潔;JavaScript適合前端開發,學習曲線較陡,語法靈活。 1.Python語法直觀,適用於數據科學和後端開發。 2.JavaScript靈活,廣泛用於前端和服務器端編程。

Python和JavaScript在社區、庫和資源方面的對比各有優劣。 1)Python社區友好,適合初學者,但前端開發資源不如JavaScript豐富。 2)Python在數據科學和機器學習庫方面強大,JavaScript則在前端開發庫和框架上更勝一籌。 3)兩者的學習資源都豐富,但Python適合從官方文檔開始,JavaScript則以MDNWebDocs為佳。選擇應基於項目需求和個人興趣。

從C/C 轉向JavaScript需要適應動態類型、垃圾回收和異步編程等特點。 1)C/C 是靜態類型語言,需手動管理內存,而JavaScript是動態類型,垃圾回收自動處理。 2)C/C 需編譯成機器碼,JavaScript則為解釋型語言。 3)JavaScript引入閉包、原型鍊和Promise等概念,增強了靈活性和異步編程能力。

不同JavaScript引擎在解析和執行JavaScript代碼時,效果會有所不同,因為每個引擎的實現原理和優化策略各有差異。 1.詞法分析:將源碼轉換為詞法單元。 2.語法分析:生成抽象語法樹。 3.優化和編譯:通過JIT編譯器生成機器碼。 4.執行:運行機器碼。 V8引擎通過即時編譯和隱藏類優化,SpiderMonkey使用類型推斷系統,導致在相同代碼上的性能表現不同。

JavaScript在現實世界中的應用包括服務器端編程、移動應用開發和物聯網控制:1.通過Node.js實現服務器端編程,適用於高並發請求處理。 2.通過ReactNative進行移動應用開發,支持跨平台部署。 3.通過Johnny-Five庫用於物聯網設備控制,適用於硬件交互。

我使用您的日常技術工具構建了功能性的多租戶SaaS應用程序(一個Edtech應用程序),您可以做同樣的事情。 首先,什麼是多租戶SaaS應用程序? 多租戶SaaS應用程序可讓您從唱歌中為多個客戶提供服務

本文展示了與許可證確保的後端的前端集成,並使用Next.js構建功能性Edtech SaaS應用程序。 前端獲取用戶權限以控制UI的可見性並確保API要求遵守角色庫

JavaScript是現代Web開發的核心語言,因其多樣性和靈活性而廣泛應用。 1)前端開發:通過DOM操作和現代框架(如React、Vue.js、Angular)構建動態網頁和單頁面應用。 2)服務器端開發:Node.js利用非阻塞I/O模型處理高並發和實時應用。 3)移動和桌面應用開發:通過ReactNative和Electron實現跨平台開發,提高開發效率。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3漢化版
中文版,非常好用

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),