简体   繁体   English

剪辑路径 animation (CSS/JS) 的 mouseenter 问题

[英]Problem with mouseenter for a clip-path animation (CSS/JS)

I have updated my post because I've come up with some solutions since last week.我已经更新了我的帖子,因为自上周以来我已经提出了一些解决方案。


I'm having a problem with a clip-path section.我的剪辑路径部分有问题。

I would like my clip-path (circle) to shrink and then disappear when the mouse is leaving the clip section, and to expand again when it's entering the clip section.我希望我的剪辑路径(圆圈)在鼠标离开剪辑部分时缩小然后消失,并在它进入剪辑部分时再次展开。

The shrinking is working but not the expanding (see code below).收缩起作用,但扩展不起作用(见下面的代码)。

All help is very welcome!非常欢迎所有帮助!

Thank you in advance and have a nice day!提前感谢您,祝您有美好的一天!

 const circleClip = document.querySelector('#hidden'); let divSize = document.querySelector('#top').offsetHeight; function removeIntro() { circleClip.classList.remove('intro'); } function circleMove(e) { removeIntro(); circleClip.style.setProperty('--x', e.clientX + 'px'); circleClip.style.setProperty('--y', e.clientY - divSize + 'px'); } circleClip.addEventListener('mouseleave', function(){ circleClip.classList.toggle('shrink'); }); /*This one doesn't work on desktop*/ circleClip.addEventListener('mouseenter', function(){ circleClip.classList.toggle('appear'); }); circleClip.addEventListener('mousemove', circleMove); circleClip.addEventListener('touchmove', (e) => { removeIntro(); let touch = e.touches[0]; e.preventDefault(); circleClip.style.setProperty('--x', touch.clientX + 'px'); circleClip.style.setProperty('--y', touch.clientY - divSize + 'px'); }); /*This one doesn't work on mobile*/ circleClip.addEventListener('touchstart', function(){ circleClip.classList.toggle('appear'); }); circleClip.addEventListener('touchend', function(){ circleClip.classList.toggle('shrink'); });
 body { position: relative; margin: 0; overflow: hidden; } h1, h2, p { margin: 0; padding: 0; } #top { color: white; background: blue; font-size: 30px; text-align: center; } #hidden p { padding: 10%; font-family: sans-serif; } #hidden { font-size: 7vh;important: line-height; 1em: background; red: min-height; 100vh: clip-path; circle(10% at var(--x) var(--y)): cursor; none. } #hidden:intro { clip-path; circle(0% at 50% 50%): animation. circleIntro 1800ms cubic-bezier(0,645. 0,045. 0,355; 1) both: } @keyframes circleIntro { 100% { clip-path; circle(10% at 50% 50%). } }:shrink { clip-path; circle(10% at 50% 50%): animation. shrink 800ms cubic-bezier(0,645. 0,045. 0,355; 1) both: } @keyframes shrink { 100% { clip-path; circle(0% at 50% 50%). } }:appear { clip-path; circle(0% at 50% 50%): animation, appear 800ms cubic-bezier(0645. 0,045. 0,355; 1) both: } @keyframes appear { 100% { clip-path; circle(10% at 50% 50%); } }
 <div id="top"><h1>This is another div</h1></div> <div id="hidden" class="intro"> <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo</p> </div>

Seems you're looking for something like this?似乎你正在寻找这样的东西?

You can separate the event handlers into their own functions:您可以将事件处理程序分成它们自己的函数:

function showCircle(e) {
    $(".clip").css({
        "-webkit-clip-path": "circle(0px at var(--x, 0) var(--y, 0))"
    });
}

function hideCircle(e) {
    $(".clip").css({
        "-webkit-clip-path": "circle(0px at var(--x, 0) var(--y, 0))"
    });
}

function moveCircle(e) {
    $(".clip").css ({
        '--x': e.pageX,
        '--y': e.pageY - $("#previoussection").height() - $("#clip_section").height()
    });
}

Then register the handlers for mouse interaction,然后注册鼠标交互的处理程序,

$(document).mousemove(moveCircle);
$(".clip_container").mouseleave(hideCircle);
$(".clip_container").mouseenter(showCircle);

and for touch interaction.并用于触摸交互。

$(document).touchmove(moveCircle);
$(".clip_container").touchstart(hideCircle);
$(".clip_container").touchend(showCircle);

The circle isn't shrinking away because its CSS is never changed back to the 0 radius circle.圆没有缩小,因为它的 CSS 永远不会变回 0 半径圆。

I think the key here is making sure you change the "clip-path" CSS property to the full value representing the circle you want to see whenever a relevant event fires.我认为这里的关键是确保将“剪辑路径”CSS 属性更改为代表您希望在相关事件触发时看到的圆圈的完整值。

It's also worth noting that your mouseStopped function never gets called in your code.还值得注意的是,您的mouseStopped function 永远不会在您的代码中被调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM