gpt4 book ai didi

带有 translate3d 的 Javascript + jQuery 图像视差滞后

转载 作者:太空宇宙 更新时间:2023-11-04 09:08:01 26 4
gpt4 key购买 nike

我创建了一个小脚本来确定光标在图像上的位置并用它稍微移动图片,我尝试用 translate3d() 来实现这个但它有点滞后,只有当你停止移动时才会停止滞后你的光标。

JSFiddle:https://jsfiddle.net/c31k00ve/1/

灵感(警告:声音):https://html.nkdev.info/godlike/gallery.html

JS:

(function($) {
var $gallery = $('.gallery');
var relX, relY, $e;
$gallery.on('mousemove', function(e) {
$e = e;
window.requestAnimationFrame(test);
});

var test = function () {
var $container = $('.container');
var offset = $container.offset();
relX = $e.pageX - offset.left - $container.width() / 2;
relY = $e.pageY - offset.top - $container.height() / 2;
$gallery.css({
transform: "translate3d(" + (relX * 0.1) + "px, " + (relY * 0.1) + "px, 0) scale(1.3)"
});
}
})(jQuery);

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Development</title>
<link rel="stylesheet" type="text/css" href="/style.css" />
<script type="text/javascript" src="/jQuery.js"></script>
</head>
<body>
<div class="container">
<div class="gallery"></div>
</div>
<span class="xcoord">a</span>
<span class="ycoord">a</span>
<script type="text/javascript" src="/script.js"></script>
</body>
</html>

CSS:

body {
margin: 0;
}

.container {
position: relative;
margin: 50px auto;
width: 200px;
}
.gallery {
height: 200px;
width: 200px;
background-image: url(https://placekitten.com/g/200/200);
transition: 0.5s;
transform: translate3d(0, 0, 0);
-webkit-transform: translate3d(0, 0, 0);
transform-style: preserve-3d;
-webkit-transform-style: preserve-3d;
backface-visibility: hidden;
-webkit-backface-visibility: hidden;
}
.gallery:hover {
transform: scale(1.2);
transition: 0.5s;
}
.xcoord, .ycoord {
display: block;
}

最佳答案

您需要删除过渡属性。我明白为什么你在开始时为了悬停效果而把它放在那里,但也许可以通过不同的方式实现。

根据我的建议更新了 fiddle https://jsfiddle.net/c31k00ve/4/

.gallery {
height: 200px;
width: 200px;
background-image: url(https://placekitten.com/g/200/200);
transform: translate3d(0, 0, 0);
-webkit-transform: translate3d(0, 0, 0);
transform-style: preserve-3d;
-webkit-transform-style: preserve-3d;
backface-visibility: hidden;
-webkit-backface-visibility: hidden;
}
.gallery:hover {
transform: scale(1.2);
}

关于带有 translate3d 的 Javascript + jQuery 图像视差滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42330333/

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