[我正在努力寻找如何在iframe上方隐藏自定义光标的方法。
我设计了一个自定义游标,但在所有Web部分中都可以正常工作。但是,当它越过Vimeo iframe时,鼠标停留在iframe的边缘并显示默认的Web浏览器光标。
[我认为最简单的方法是,每当我将鼠标悬停在iframe上时,都将隐藏自定义光标。
[试图通过使用CSS(自定义光标位于iframe上时不使用display:none)和js来解决,但没有解决。
这里是代码笔中的代码:https://codepen.io/felixgonzalo/pen/vYOLrVJ
这是代码:JS
let mouseCursor = document.querySelector(".cursor");
let Links = document.querySelectorAll("a");
let logo = document.querySelector(".logo-error");
window.addEventListener('mousemove', cursor);
function cursor(e){
mouseCursor.style.top = e.pageY + "px";
mouseCursor.style.left = e.pageX + "px";
}
Links.forEach(link =>{
if ( link !== logo ){
link.addEventListener("mouseleave", () => {
mouseCursor.classList.remove("link-grow");
});
link.addEventListener("mouseover", () => {
mouseCursor.classList.add("link-grow");
});
}
});
CSS
body{
cursor: none;
}
.cursor{
width: 2rem;
height: 2rem;
border: 2px solid white;
border-radius: 50%;
position: absolute;
transform: translate(-50%, -50%);
transition: all 0.3s ease;
transition-property: background, transform;
transform-origin: 100% 100%;
z-index: 20000;
pointer-events: none;
}
.link-grow{
transform: scale(1.2);
background: white;
mix-blend-mode: difference;
}
a:-webkit-any-link {
cursor: none;
}
.logo-error:hover .cursor{
display: none !important;
}
@media (max-width: 768px){
.cursor {
display: none;
}
}
基本上,您需要在这里做三件事:
iframe
元素var iframe = document.querySelector("iframe");
mouseover
事件处理程序iframe.addEventListener("mouseover", function() {
mouseCursor.style.display = 'none';
})
mouseleave
事件处理程序iframe.addEventListener("mouseleave", function() {
mouseCursor.style.display = 'block';
})
[现在,只要将鼠标悬停在iframe上,您的自定义光标就会是hidden
,一旦将鼠标移离iframe,它就会再次显示。
请注意:请注意,我使用的querySelector
仅返回FIRST选择器,因此,如果您有很多iFrame,它将仅将代码应用于第一个。为了避免这种情况,可以使用querySelectorAll
或getElementsByTagName
,循环返回的数组,然后注入事件。