如何定位图像元素使其跟随在光标上方?
How can I position an image element so it follows above the cursor?
所以我正在尝试做一些奇怪的具体事情,我被要求为某人制作一个网站作为一个小插科打,我想知道这是否可以通过 javascript/css 实现,你能定位这个吗图像,以便它出现在光标上方的特定位置?我想让 luigi gif 出现在 goomba 上,这样它看起来就像被踩了一样。我试过很多东西,但我尝试的任何东西都会破坏它。
我是 javascript 的初学者!这对我来说比 HTML 和 CSS.
难多了
function moveImg(event) {
var x = event.clientX;
var y = event.clientY;
var luigi = document.getElementById("luigi");
luigi.style.left = x+'px';
luigi.style.top = y+'px';
}
html {
cursor: url("https://cdn.discordapp.com/attachments/975528286711078942/981315329357676585/goomber.gif"), auto;
padding: 0;
margin: 0;
}
body {
background-image: url(https://cdn.discordapp.com/attachments/975528286711078942/981314082114269294/fire.gif);
margin:0px;
padding:0px;
font-family: 'Roboto', sans-serif;\
position: relative;
height: 100vh;
}
div {
width: 100%;
height: 100%;
border: none;
}
#header {
}
img {
position: absolute;
object-position: x y;
}
<html>
<head>
<style> </style>
</head>
<body>
<center>
<img style="pointer-events:none;user-select:none;" src="https://cdn.discordapp.com/attachments/975528286711078942/981314061809647706/killhim.gif">
</center>
<br>
<div onmousemove="moveImg(event)">
<img id="luigi" src='https://cdn.discordapp.com/attachments/975528286711078942/981314102783799296/weegeestomp.gif'>
</div>
<script></script>
</body>
</html>
它的外观图片
var cursor = $(".cursor"),
follower = $(".cursor-follower");
var posX = 0,
posY = 0;
var mouseX = 0,
mouseY = 0;
TweenMax.to({}, 0.016, {
repeat: -1,
onRepeat: function() {
posX += (mouseX - posX) / 9;
posY += (mouseY - posY) / 9;
TweenMax.set(follower, {
css: {
left: posX - 12,
top: posY - 12
}
});
TweenMax.set(cursor, {
css: {
left: mouseX,
top: mouseY
}
});
}
});
$(document).on("mousemove", function(e) {
mouseX = e.clientX;
mouseY = e.clientY;
});
$(".link").on("mouseenter", function() {
cursor.addClass("active");
follower.addClass("active");
});
$(".link").on("mouseleave", function() {
cursor.removeClass("active");
follower.removeClass("active");
});
body {
width: 100%;
height: 100vh;
background-image: linear-gradient(to top, #007adf 0%, #00ecbc 100%);
font-family: sans-serif;
overflow: hidden;
cursor: none;
}
.cursor {
position: absolute;
background-image:url('https://cdn.discordapp.com/attachments/975528286711078942/981315329357676585/goomber.gif');
background-size:contain;
background-repeat:no-repeat;
width: 40px;
height: 40px;
z-index: 1;
transition: 0.3s cubic-bezier(0.75, -1.27, 0.3, 2.33) transform,
0.2s cubic-bezier(0.75, -0.27, 0.3, 1.33) opacity;
user-select: none;
pointer-events: none;
z-index: 10000;
transform: scale(1);
&.active {
opacity: 0.5;
transform: scale(0);
}
&.hovered {
opacity: 0.08;
}
}
.cursor-follower {
position: absolute;
background-image:url('https://cdn.discordapp.com/attachments/975528286711078942/981314102783799296/weegeestomp.gif');
background-size:contain;
background-repeat:no-repeat;
width:100px;
height:200px;
z-index: 2;
user-select: none;
pointer-events: none;
z-index: 10000;
transform: translate(0, -90px);
&.active {
opacity: 0.7;
transform: scale(3);
}
&.hovered {
opacity: 0.08;
}
}
a {
text-decoration: none;
text-transform: uppercase;
color: white;
font-size: 11px;
letter-spacing: 1px;
}
.link-list {
position: absolute;
bottom: 0;
left: 0;
list-style: none;
&__item {
display: inline-block;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/latest/TweenMax.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="cursor"></div>
<div class="cursor-follower"></div>
<ul class="link-list">
<li class="link-list__item">
<a href="#" class="link">Hover me</a>
</li>
</ul>
所以我正在尝试做一些奇怪的具体事情,我被要求为某人制作一个网站作为一个小插科打,我想知道这是否可以通过 javascript/css 实现,你能定位这个吗图像,以便它出现在光标上方的特定位置?我想让 luigi gif 出现在 goomba 上,这样它看起来就像被踩了一样。我试过很多东西,但我尝试的任何东西都会破坏它。 我是 javascript 的初学者!这对我来说比 HTML 和 CSS.
难多了function moveImg(event) {
var x = event.clientX;
var y = event.clientY;
var luigi = document.getElementById("luigi");
luigi.style.left = x+'px';
luigi.style.top = y+'px';
}
html {
cursor: url("https://cdn.discordapp.com/attachments/975528286711078942/981315329357676585/goomber.gif"), auto;
padding: 0;
margin: 0;
}
body {
background-image: url(https://cdn.discordapp.com/attachments/975528286711078942/981314082114269294/fire.gif);
margin:0px;
padding:0px;
font-family: 'Roboto', sans-serif;\
position: relative;
height: 100vh;
}
div {
width: 100%;
height: 100%;
border: none;
}
#header {
}
img {
position: absolute;
object-position: x y;
}
<html>
<head>
<style> </style>
</head>
<body>
<center>
<img style="pointer-events:none;user-select:none;" src="https://cdn.discordapp.com/attachments/975528286711078942/981314061809647706/killhim.gif">
</center>
<br>
<div onmousemove="moveImg(event)">
<img id="luigi" src='https://cdn.discordapp.com/attachments/975528286711078942/981314102783799296/weegeestomp.gif'>
</div>
<script></script>
</body>
</html>
它的外观图片
var cursor = $(".cursor"),
follower = $(".cursor-follower");
var posX = 0,
posY = 0;
var mouseX = 0,
mouseY = 0;
TweenMax.to({}, 0.016, {
repeat: -1,
onRepeat: function() {
posX += (mouseX - posX) / 9;
posY += (mouseY - posY) / 9;
TweenMax.set(follower, {
css: {
left: posX - 12,
top: posY - 12
}
});
TweenMax.set(cursor, {
css: {
left: mouseX,
top: mouseY
}
});
}
});
$(document).on("mousemove", function(e) {
mouseX = e.clientX;
mouseY = e.clientY;
});
$(".link").on("mouseenter", function() {
cursor.addClass("active");
follower.addClass("active");
});
$(".link").on("mouseleave", function() {
cursor.removeClass("active");
follower.removeClass("active");
});
body {
width: 100%;
height: 100vh;
background-image: linear-gradient(to top, #007adf 0%, #00ecbc 100%);
font-family: sans-serif;
overflow: hidden;
cursor: none;
}
.cursor {
position: absolute;
background-image:url('https://cdn.discordapp.com/attachments/975528286711078942/981315329357676585/goomber.gif');
background-size:contain;
background-repeat:no-repeat;
width: 40px;
height: 40px;
z-index: 1;
transition: 0.3s cubic-bezier(0.75, -1.27, 0.3, 2.33) transform,
0.2s cubic-bezier(0.75, -0.27, 0.3, 1.33) opacity;
user-select: none;
pointer-events: none;
z-index: 10000;
transform: scale(1);
&.active {
opacity: 0.5;
transform: scale(0);
}
&.hovered {
opacity: 0.08;
}
}
.cursor-follower {
position: absolute;
background-image:url('https://cdn.discordapp.com/attachments/975528286711078942/981314102783799296/weegeestomp.gif');
background-size:contain;
background-repeat:no-repeat;
width:100px;
height:200px;
z-index: 2;
user-select: none;
pointer-events: none;
z-index: 10000;
transform: translate(0, -90px);
&.active {
opacity: 0.7;
transform: scale(3);
}
&.hovered {
opacity: 0.08;
}
}
a {
text-decoration: none;
text-transform: uppercase;
color: white;
font-size: 11px;
letter-spacing: 1px;
}
.link-list {
position: absolute;
bottom: 0;
left: 0;
list-style: none;
&__item {
display: inline-block;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/latest/TweenMax.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="cursor"></div>
<div class="cursor-follower"></div>
<ul class="link-list">
<li class="link-list__item">
<a href="#" class="link">Hover me</a>
</li>
</ul>