拖动时未触发Safari仅CSS悬停事件

时间:2018-09-02 03:55:29

标签: css safari hover mousedown onmousedown

Safari在css中注册悬停事件似乎有些问题。如果运行下面的代码片段并将光标从蓝色拖到绿色,则应该发生两件事。在所有浏览器上,绿色div会在悬停时变为红色。在非Safari浏览器(firefox和chrome,均为最新)上,当从蓝色拖动到绿色时,当光标进入时,绿色div将变为红色。在Safari(也是最新的)上,将光标从蓝色div拖动到绿色div时,绿色div不会变成红色。当鼠标已经按下时,识别悬停似乎是一个问题。我尝试了许多不同的变体和其他解决方案,但是它们不起作用(设置其他css属性以使其重新绘制等等)。谁能解释这种奇怪的行为以及如何解决/解决它?

div {
  position: fixed;
  color: white;
  -webkit-user-select: none;
  user-select: none;
}
div.blue {
  top: 0;
  left: 0;
  width: 50%;
  height: 100%;
  background-color: blue;
}
div.green {
  top: 0;
  left: 50%;
  width: 50%;
  height: 100%;
  background-color: green;
}
.green:hover {
  background-color: red !important;
}
<div class="blue">CLICK HERE</div>
<div class="green">AND DRAG HERE</div>

1 个答案:

答案 0 :(得分:1)

经过更多搜索后,我发现我必须使用javascript鼠标进入和鼠标离开事件来更改颜色。似乎是故意将Safari悬停为无法将拖动识别为悬停。

'use strict';
function random(min, max) {
  return Math.random() * (max - min) + min;
}
   /*
    * ------------------------------------------
    * *-----------------------------
    *  User Event
    * *-----------------------------
    * ------------------------------------------
    */
   class UserEvent {
      constructor(canvasBody) {
         this.UP_ARROW = 38 || 87,
         this.RIGHT_ARROW =39 || 68,
         this.DOWN_ARROW = 37 || 83,
         this.LEFT_ARROW = 40 || 65,
         this.keys = []

         canvasBody.addEventListener('keydown', (e) => {
            this.keys[e.keyCode] = true;
         });

         canvasBody.addEventListener('keyup', (e) => {
            this.keys[e.keyCode] = false;
         });
      }

      checkKey(key) {
         return this.keys[key];
      }

//       ufoMove() {
//          this.canvasBody.checkKey(this.UP_ARROW)
//          this.canvasBody.checkKey(this.RIGHT_ARROW)
//          this.canvasBodycheckKey(this.DOWN_ARROW)
//          this.canvasBody.checkKey(this.LEFT_ARROW)
//          console.log(this.canvasBody.leftArrow = this.checkKey(this.LEFT_ARROW))

//          if(this.UP_ARROW) {
//             this.x += this.x * this.velocity.x
//          }
//       }
   }


   /*
    * ------------------------------------------
    * *-----------------------------
    *  UFO
    * *-----------------------------
    * ------------------------------------------
    */
   class Ufo {
      constructor(x, y) {
         this.x = x,
         this.y = y,
         this.velocity = {
            x: 3,
            y: 3
         }
      }

      draw(c) {
         c.save()
         c.beginPath()
         c.arc(this.x, this.y, 50, 0, Math.PI * 2, false)
         c.fillStyle = "#fff";
         c.shadowColor = "#e3eaef";
         c.shadowBlur = 20;
         c.fill()
         c.closePath()
         c.restore()
      }

      update(c) {
         this.draw(c)
         // Get the keys first
         // this.EventUser.ufoMove(c);
         this.x = this.x + random(-5,5);
         this.y = this.y + random(-5,5);
         //    this.x = this.x + this.velocity.x;
         // }
      }
   }

   /*
    * ------------------------------------------
    * *-----------------------------
    *  Canvas
    * *-----------------------------
    * ------------------------------------------
    */      
   class CanvasDisplay {
      constructor() {
         this.canvas = document.querySelector('canvas');
           this.ctx = this.canvas.getContext('2d');
         this.stageConfig = {
              width: window.innerWidth,
              height: window.innerHeight
         };         
         this.canvas.width = this.stageConfig.width;
         this.canvas.height = this.stageConfig.height;

         this.backgroundGradient = this.ctx.createLinearGradient(0, 0, 0, this.canvas.height);
         this.backgroundGradient.addColorStop(0, '#171e26');
         this.backgroundGradient.addColorStop(1, '#3f586b');

         this.Ufo = new Ufo(this.canvas.width / 2, this.canvas.height / 2);
         this.UserEvent = new UserEvent(document.body);
      }

      animate() {
         this.ctx.fillStyle = this.backgroundGradient;
         this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);

         this.Ufo.update(this.ctx)
         window.requestAnimationFrame(this.animate);
      }
   }

   let canvasDisplay = new CanvasDisplay();
   canvasDisplay.animate();
document.getElementsByTagName('div')[1].onmouseenter = () => {
  document.getElementsByTagName('div')[1].classList.add('hover')
}

document.getElementsByTagName('div')[1].onmouseleave = () => {
  document.getElementsByTagName('div')[1].classList.remove('hover')
}
div {
  position: fixed;
  color: white;
  -webkit-user-select: none;
  user-select: none;
}
div.blue {
  top: 0;
  left: 0;
  width: 50%;
  height: 100%;
  background-color: blue;
}
div.green {
  top: 0;
  left: 50%;
  width: 50%;
  height: 100%;
  background-color: green;
}
.green:hover, .green.hover {
  background-color: red !important;
}