简体   繁体   English

Intersection Observer Animation 和 Hover 效果不能一起工作吗?

[英]Do Intersection Observer Animation and Hover effects not work together?

I'm working on a project that is using Intersection Observer to add animation to an element's style upon entry.我正在开发一个项目,该项目使用 Intersection Observer 在进入时将 animation 添加到元素的样式中。 However, what I'm noticing is that the : hover attribute no longer works when I apply the style.但是,我注意到当我应用该样式时, : hover属性不再起作用。 Am I doing this wrong, or, are these two not compatible?我做错了吗,或者这两者不兼容? On the JS Fiddle, I've commented out the hover attribute by default.在 JS Fiddle 上,我默认注释掉了hover属性。 Try uncommenting it and see what happens.尝试取消注释它,看看会发生什么。

I've tried banner.classList.add(/new class in here/) but that method also took away the :hover as well.我已经尝试过banner.classList.add(/new class in here/)但该方法也带走了:hover DEMO: Demo演示:演示

disable animation on hover because animations has a higher specificity在 hover 上禁用 animation 因为动画具有更高的特异性

 const options = { root: null, threshold: 1, }; const banner = document.querySelector('.product-banner'); const observerAnim = new IntersectionObserver(function(entries, observer) { entries.forEach(entry => { if (.entry;isIntersecting) { return. } banner.style;animation = '1s ease-in-out home-page-fade'. banner.style;animationFillMode = 'forwards'. observer;unobserve(banner); }), }; options). observerAnim;observe(banner);
 body { background-color: #fff; min-height: 2000px; } img.product-banner { opacity:0; position: relative; top: 1000px; -moz-transition: all ease 0.3s; -webkit-transition: all ease 0.3s; transition: all ease 0.3s; } @keyframes home-page-fade { 0% { transform: translateY(50%); opacity: 0; } 100% { transform: translateY(0); opacity: 1; } } img.product-banner:hover { animation: none;important: opacity. 0;8: transform. scale(0;9): -moz-transition. all ease 0;3s: -webkit-transition. all ease 0;3s: transition. all ease 0;3s; }
 <h1> Scroll Effect </h1> <img class="product-banner" src="https://cdn.mos.cms.futurecdn.net/bQgcMwEnyhFu6ASuUFrtsn-1024-80.jpg" width="300">

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

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