gpt4 book ai didi

javascript - 动画光标不适用于 anchor

转载 作者:行者123 更新时间:2023-11-28 04:29:04 27 4
gpt4 key购买 nike

我已经搜索了几个小时,但似乎找不到答案。我正在尝试调整这个codepen https://codepen.io/Nharox/pen/akgEQm可以使用图像和链接,但有两件事不起作用。一是在使用浏览器或鼠标滚轮滚动后,光标的位置与鼠标指针应在的垂直位置不匹配,二是单击链接没有效果。我似乎无法弄清楚为什么。

<body>
<div class="cursor hidden">
<div class="cross">
<div class="b b1"></div>
<div class="b b2"></div>
</div>
<svg class="circle" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid" width="52" height="52" viewBox="0 0 52 52">
<path d="M1,26a25,25 0 1,0 50,0a25,25 0 1,0 -50,0"/>
</svg>
</div>

链接

<div class="myrow">
<div class="myrow__box">
<a href="/link-to-page"><img alt="" src="/images/myimage.jpg" /></a>
</div>
</div>

SCSS

.cursor {
position: absolute;
z-index: 5;
width: 50px;
height: 50px;
opacity: 0;
transform: translate3d(-50%, -50%, 0) scale(.9) rotate(135deg);
transition: opacity 0.5s, transform 0.5s;
pointer-events: none;
&:before, &:after {
content: '';
position: absolute;
top: 50%;
left: 50%;
width: 3px;
height: 3px;
background-color: white;
transition: width 0.5s;
}
&:before {
transform: translate3d(-50%, -50%, 0);
}
&:after {
transform: translate3d(-50%, -50%, 0) rotate(90deg);
transform-origin: center;
}
svg {
fill: transparent;
stroke: white;
stroke-width: 3;
stroke-dasharray: 160;
stroke-dashoffset: 160;
overflow: visible;
transition: stroke-dashoffset 0.5s;
}
&-is-visible {
opacity: 1;
transform: translate3d(-50%, -50%, 0) scale(1) rotate(0deg);
&:before, &:after {
width: 22px;
}
svg {
stroke-dashoffset: 0;
}
}
}
.myrow {
display: block;
max-width: 100%;
margin: 0 auto;
&__box {
cursor: none;
position: relative;
height: auto;
transform: scale(1);
&:active {
&:before {
background-color: rgba(black, 0.15);
}
}
&:before {
content: '';
position: absolute;
z-index: 1;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: rgba(black, 0);
transition: background-color 0.3s;
}
}
}

JS

(function showCursor() {
'use strict';
// Variables
var boxes = document.querySelectorAll('.myrow__box'),
cursor = document.querySelector('.cursor'),
boxPos = [];
// Get coordinates for the current cursor position
function getPos(e, el) {
var xPos = 0,
yPos = 0;
xPos = (el.offsetLeft - el.scrollLeft + el.clientLeft);
yPos = (el.offsetTop - el.scrollTop + el.clientTop);
var mouseX = e.clientX - xPos,
mouseY = e.clientY - yPos;
cursor.style.top = '' + mouseY + 'px';
cursor.style.left = '' + mouseX + 'px';
}
// Add event listeners and call fns for the corresponding box
for (var i = 0; i < boxes.length; i++) {
boxes[i].addEventListener('mousemove', function(event) {
var currentBox = this;
boxPos = getPos(event, currentBox);
}, false);
boxes[i].addEventListener('mouseenter', function() {
this.appendChild(cursor);
setTimeout(function() {
cursor.classList.add('cursor-is-visible')
}, 10);
}, false);
boxes[i].addEventListener('mouseleave', function() {
cursor.classList.remove('cursor-is-visible');
}, false);
}
})();

我需要任何尺寸的图像都是浏览器宽度的 100%。光标在其他方面工作正常,并且动画效果与代码笔完全相同。只是没有可点击的链接和错误的 y 位置。

最佳答案

首先,Y 位置是正确的,因为您没有注册事件监听器来相应更新您的 Y 位置。到目前为止,您只有鼠标 - 进入、移动和离开事件。为了考虑由于滚动而发生的光标位置变化(顺便说一句,这不是光标事件),您需要注册一个 scoll 事件并相应地更新光标 Y 位置值。

其次, anchor 没有做任何事情,因为它像大海捞针一样迷失了。为了使其工作,您可以将“box”元素包装在 anchor 标记中。

这是更改后的 HTML:

<a href="www.google.com" target="_blank">
<div class="myrow">
<div class="myrow__box">
<img alt="" src="https://www.easyjet.com/en/holidays/shared/images/guides/austria/vienna.jpg" />
</div>
</div>
</a>

这是更改后的 JS:

(function showCursor() {
'use strict';
// Variables
var boxes = document.querySelectorAll('.myrow__box'),
cursor = document.querySelector('.cursor'),
boxPos = [],
scrollY = 0;
// Get coordinates for the current cursor position
function getPos(e, el) {
var xPos = 0,
yPos = 0;
xPos = (el.offsetLeft - el.scrollLeft + el.clientLeft);
yPos = (el.offsetTop - el.scrollTop + el.clientTop);
var mouseX = e.clientX - xPos,
mouseY = e.clientY - yPos + scrollY;
cursor.style.top = '' + mouseY + 'px';
cursor.style.left = '' + mouseX + 'px';
}
// Add event listeners and call fns for the corresponding box
for (var i = 0; i < boxes.length; i++) {
boxes[i].addEventListener('mousemove', function(event) {
var currentBox = this;
boxPos = getPos(event, currentBox);
}, false);
boxes[i].addEventListener('mouseenter', function() {
this.appendChild(cursor);
setTimeout(function() {
cursor.classList.add('cursor-is-visible')
}, 10);
}, false);
boxes[i].addEventListener('mouseleave', function() {
cursor.classList.remove('cursor-is-visible');
}, false);
}
window.addEventListener('scroll', function (event) {
if(event.target.scrollingElement.localName == "body") {
scrollY = event.target.scrollingElement.scrollTop;
}
})
})();

查看 pen我为您创建的,以便您查看我所做的更改。

关于javascript - 动画光标不适用于 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44780315/

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