gpt4 book ai didi

javascript - JS : Add easing to moving element

转载 作者:行者123 更新时间:2023-12-03 16:47:07 26 4
gpt4 key购买 nike

我正在一个有一个中心元素的网站上工作,我希望这个元素与光标交互。这是我到目前为止所拥有的:

var hello = document.getElementsByClassName("hello");
document.onmousemove = function() {
var x = event.clientX * 100 / window.innerWidth + "%";
var y = event.clientY * 100 / window.innerHeight + "%";

for(var i=0;i<hello.length;i++) {
hello[i].style.left = x;
hello[i].style.top = y;
hello[i].style.transform = "translate(-" + x + ",-" + y + ")";
}
}
* {
padding: 0;
margin: 0;
}

body {
background-color: black;
}

.hello_wrapper_wrapper {
position: absolute;
background-color: none;
/* background-color: blue */
width: 35vw;
/* This object has to be larger than: width: 300px; height: 150px; */
height: 35vh;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

.hello_wrapper {
position: relative;
background-color: none;
width: 35vw;
height: 35vh;
}

.hello {
position: absolute;
background-color: white;
width: 300px;
height: 150px;
text-align: center;
line-height: 150px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<!doctype html>
<html lang="EN">

<head>
<meta charset="UTF-8">
<title> 0 </title>
<link rel="stylesheet" href="style.css">
</head>

<body>
<div class="hello_wrapper_wrapper">
<div class="hello_wrapper">
<div class="hello">
Hello World!
</div>
</div>
</div>
<script src="script.js"></script>
</body>

</html>

注意:主要元素在a内部: wrapper (width: 35vw; height: 35vh;) ,主要元素,称为: hello具有以下尺寸: hello (width: 300px; height: 150px;) . – hello必须大于 wrapper为此,我会在查看 fiddle 时更改预览窗口的大小。
我想为元素 添加某种缓动, 我不希望它跳来跳去或移动得太快,我希望它有点迟钝,跟随光标,但稍微往后拖一点。另外,当光标没有悬停在页面上时,页面会重新加载,然后光标进入页面,框会跳转,我想让这更流畅。
如果有人可以帮助我,我将非常感激。谢谢!
致谢: https://www.youtube.com/watch?v=AixAmLWzXYg (这是我遵循的教程。)

最佳答案

您可以使用 CSS 过渡属性来确保它是平滑的。
我尝试添加 transition-delay: 10ms , transition-timing-function: ease到 hello 元素 CSS。更多信息:https://css-tricks.com/ease-out-in-ease-in-out/在这里https://www.w3schools.com/css/css3_transitions.asp

   * {
padding: 0;
margin: 0;
}

body {
background-color: black;
}

.hello_wrapper_wrapper {
position: absolute;

background-color: none; /* background-color: blue */
width: 35vw; /* This object has to be larger than: width: 300px; height: 150px; */
height: 35vh;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

.hello_wrapper {
position: relative;

background-color: none;
width: 35vw;
height: 35vh;
}

.hello {
position: absolute;

background-color: white;
width: 300px;
height: 150px;
text-align: center;
line-height: 150px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
transition-delay: 10ms;
transition-timing-function: ease;

}
现在要确保盒子不会移动,您可以尝试使用 onmouseout body 上的事件监听器。
当鼠标离开窗口时,试试这个 javascript 代码使过渡平滑:
var hello = document.getElementsByClassName("hello");


document.onmouseleave = function(){
for(var i=0;i<2;i++){
hello[i].style.left = '50%';
hello[i].style.top = '50%';
hello[i].style.transform = "translate(-50%,-50%)";
}
document.removeEventListener('onmousemove')

}


document.onmouseenter = function(){
setTimeout(function(){
document.onmousemove = function(){
var x = event.clientX * 100 / window.innerWidth + "%";
var y = event.clientY * 100 / window.innerHeight + "%";

for(var i=0;i<2;i++){
hello[i].style.left = x;
hello[i].style.top = y;
hello[i].style.transform = "translate(-"+x+",-"+y+")";
}
}
},1000)

}

关于javascript - JS : Add easing to moving element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65454672/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com