日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

鼠标悬浮效果:css:hover;js:mouseover,mouseout

發(fā)布時(shí)間:2024/1/1 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 鼠标悬浮效果:css:hover;js:mouseover,mouseout 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

一、css樣式實(shí)現(xiàn)

二、js實(shí)現(xiàn)


一、css樣式實(shí)現(xiàn)

1.xxx:hover

只能生成css可以控制的樣式效果

<style>div:hover{color:red;} </style>

二、js實(shí)現(xiàn)

1.moseover(鼠標(biāo)懸浮)

1)能修改樣式,同時(shí)修改DOM中的內(nèi)容

<script>let sons = document.getElementsByClassName("son");let borderWidth = 4;for (let i = 0; i < sons.length; i++) {sons[i].addEventListener("mouseover", (e) => {console.log("mouseover:", e, e.target.style)e.target.style.borderWidth = borderWidth + i + "px";e.target.innerHTML="mouseover"+i+1;})sons[i].addEventListener("mouseout", (e) => {console.log("mouseout:", e, e.target.style)e.target.style.borderWidth = "none";e.target.innerHTML = (i + 1)})}</script>

2)優(yōu)化:事件委派,減少點(diǎn)擊事件的注冊(cè),減少內(nèi)存占用

?用e.target中的 className 或 tagName 或?id 判斷是否時(shí)需要更改樣式、內(nèi)容的元素

<script>let parent = document.getElementsByClassName("container");let borderWidth = 8;// 事件委托:只在父元素上注冊(cè)事件,減少內(nèi)存占用parent[0].addEventListener("mouseover", (e) => {console.log("e", e, e.target)if (e.target.className == "son") {e.target.style.boxSizing = "border-box"console.log("son:", e.target.className)e.target.style.borderWidth = borderWidth + "px";e.target.style.padding = 20 + "px";e.target.innerHTML = "mouseover" + (borderWidth)}}) </script>

3)完整代碼

<!DOCTYPE html> <html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>邊框變化不影響布局</title><style>.container {display: flex;flex-wrap: wrap;}.son {height: 400px;width: 300px;background-color: skyblue;margin: 5px;/* border: 3px solid transparent; *//* box-sizing: border-box; */}.son:hover {border: 6px solid #894;}</style> </head> <body><div class="container"><div class="son">1</div><div class="son">2</div><div class="son">3</div><div class="son">4</div><div class="son">5</div><div class="son">6</div><div class="son">7</div><div class="son">8</div></div> </body> <script>// let sons = document.getElementsByClassName("son");// let borderWidth = 4;// // object// console.log("sonsType:", typeof (sons))// for (let i = 0; i < sons.length; i++) {// sons[i].addEventListener("mouseover", (e) => {// e.target.style.boxSizing = "border-box"// console.log("mouseover:", e, e.target.style)// e.target.style.borderWidth = borderWidth + i + "px";// e.target.innerHTML = "mouseover" + (i + 1)// })// sons[i].addEventListener("mouseout", (e) => {// console.log("mouseout:", e, e.target.style)// e.target.style.borderWidth = "none";// e.target.innerHTML = (i + 1)// })// }let parent = document.getElementsByClassName("container");let borderWidth = 8;// 事件委托:只在父元素上注冊(cè)事件,減少內(nèi)存占用parent[0].addEventListener("mouseover", (e) => {console.log("e", e, e.target)if (e.target.className == "son") {//加邊框不會(huì)影響布局e.target.style.boxSizing = "border-box"console.log("son:", e.target.className)e.target.style.borderWidth = borderWidth + "px";e.target.style.padding = 20 + "px";e.target.innerHTML = "mouseover" + (borderWidth)}}) </script></html>

2.mouseout(鼠標(biāo)移除調(diào)用的事件)

? 同mouseover,不再贅述!

/*

希望對(duì)你有幫助!

如有錯(cuò)誤,歡迎指正!

非常感謝!

*/

總結(jié)

以上是生活随笔為你收集整理的鼠标悬浮效果:css:hover;js:mouseover,mouseout的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。