鼠标悬停按钮控制视频播放速度功能失效求助
Fixing Your Video Playback Speed on Hover Issue
Hey there, let's break down why your hover speed control isn't working—there are a few small but critical issues in your code:
Key Problems Identified
- You're targeting the wrong element: The
xvariable is grabbing the<div id="myVideo">container, but theplaybackRateproperty belongs to the actual<video>element inside that div. The div itself doesn't have video playback controls. - Function call syntax errors:
- In your
onmouseoverandonmouseoutattributes, you wrote the function names without parentheses (setPlaySpeedinstead ofsetPlaySpeed()). Without parentheses, the browser won't execute the function—it just references it. - There's a typo in your
onmouseouthandler: you usedgetPlaySpeedF, but your defined function is namedgetPlaySpeed.
- In your
Corrected Code
<!DOCTYPE html> <html> <head> <title>Welcome!</title> </head> <body> <div id="myVideo" class="video-background"> <video autoplay loop> <source src="space.mp4" type="video/mp4"> </video> </div> <div id="text-background"> <h1 class="a"> <a id="link1" onmouseover="setPlaySpeed()" onmouseout="getPlaySpeed()" href="home.html" style="text-decoration:none;">Welcome</a> </h1> </div> <div class="footer"> <p>~TEST~</p> </div> <script> // Get the actual video element inside the myVideo div var x = document.querySelector("#myVideo video"); function setPlaySpeed() { x.playbackRate = 0.1; } function getPlaySpeed() { x.playbackRate = 1; } </script> </body> </html>
What Changed?
- Fixed element selection: Used
document.querySelector("#myVideo video")to directly grab the<video>tag inside the container div. - Fixed function calls: Added parentheses to
setPlaySpeed()andgetPlaySpeed()in the hover attributes so the functions run when the events trigger. - Fixed typo: Changed
getPlaySpeedFtogetPlaySpeedto match the function definition.
Now when you hover over the "Welcome" link, the video should slow down to 0.1x speed, and return to normal 1x speed when you move your mouse away.
内容的提问来源于stack exchange,提问作者Nabeel Ahmed
相关产品推荐
相关产品推荐

