简体   繁体   English

当触发javascript onscroll事件时,css:hover效果不再起作用

[英]css :hover effect no longer works when javascript onscroll event is triggered

in javascript I have onscroll event that moves the div elements depending on window.pageYOffset. 在javascript中,我有onscroll事件,该事件根据window.pageYOffset移动div元素。 In the css I have a :hover that applies transform: scale(); 在CSS中,我有一个:hover,它可以应用transform:scale();

When the page loads, the css effect works. 页面加载时,css效果起作用。 However, as soon as you start scrolling and the onscroll event is applied, the divs that moves no longer respond to the hover effect. 但是,一旦开始滚动并应用了onscroll事件,移动的div就不再响应悬停效果。 The divs that don't have scroll event still have the css effect. 没有滚动事件的div仍然具有css效果。 Any idea what is causing the conflict? 知道导致冲突的原因是什么? https://jsfiddle.net/kshatriiya/b2jxg3ep/1/ https://jsfiddle.net/kshatriiya/b2jxg3ep/1/

<div id="container">
  <div class="box" id="one"></div>
  <div class="box" id="two"></div>
  <div class="box" id="three"></div>
  <div class="box" id="four"></div>
  <div class="box" id="five"></div> 
</div>

<div id="scroll-container">
  <div class="box move" data-offset="12" id="six"></div>
  <div class="box move" data-offset="14" id="seven"></div>
  <div class="box move" data-offset="16" id="eight"></div>
  <div class="box move" data-offset="18" id="nine"></div>
  <div class="box move" data-offset="20" id="ten"></div>  
</div> 

JS JS

var boxes = document.getElementsByClassName("box");
var movingBoxes = document.getElementsByClassName("move");
var boxArray = [];

(function() {
for (var i = 0; i < boxes.length; i++) {
var r = Math.floor((Math.random() * 254) + 1);
boxes[i].style.background = "rgba(" + r + "," + i*30 + "," + i*45 + ", 1)";
}
})();

var boxItem = function(el, speed) {
this.el = el;
this.speed = speed;
this.startOffset = this.speed*10;
};

boxItem.prototype.update = function(Yoff) {

var spd = (Yoff)/(this.speed/(this.startOffset/10));
var distanceRemain = this.startOffset-spd;
if (distanceRemain < 0) {distanceRemain = 0};
this.el.style.transform = "translateY(" + distanceRemain + "px)";

}

function prepareArrays() { 
for (i = 0; i < movingBoxes.length; i++) {
boxArray.push(new boxItem(movingBoxes[i], movingBoxes[i].getAttribute("data-offset")));
};
}

prepareArrays();


window.onscroll = function() {
var Yoffset = this.pageYOffset;

boxArray.forEach(function(el){

el.update(Yoffset);

});
}

css 的CSS

#container {
  width: 100vw;
  height: 200px;
  display: flex;
  flex-diretion: row;  
}

.box {  
 margin: 0;
 padding: 0;
 width: 20%;
 height: 100px;
 -webkit-transition: width 1s ease-out, transform 3s ease;
 -moz-transition: width 1s ease-out, transform 3s ease;
 -ms-transition: width 1s ease-out, transform 3s ease;
 -o-transition: width 1s ease-out, transform 3s ease;
 transition: width 1s ease-out, transform 3s ease;
 background: red;
}

.box:hover {
transform: scale(1.1, 1.1);
}

#scroll-container {

 width: 100%;
 height: 1000px;
 display: flex;
 flex-direction: row;
 justify-content: flex-start;
 align-content: flex-start;
}

您通过javascript应用的转换会覆盖悬停时的转换,因为通过js应用的样式是内联样式

It's pretty obvious, you're overriding the same CSS reference on the same element. 很明显,您要覆盖同一元素上的相同CSS参考。

Try putting !important , from the CSS .box:hover transform, that will do the trick 尝试从CSS .box:hover转换中放入!important ,就可以了

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

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