使用mousemove调整浏览一组图像的速度

时间:2019-07-13 19:42:10

标签: javascript jquery shuffle mousemove

我在移动鼠标的同时浏览一组图像。图像应以随机顺序翻转。但是更改本身的方法太快了,似乎它正在更改每个像素。

我尝试在简单的if条件下调整速度,但似乎不起作用。

我的代码如下:

var slideshow = $(".introImages");
var images = $(".introImages img");
var pos = { x: 0, y: 0 };

$(slideshow).on("mousemove", function(event) {
  var x = event.pageX;
  var width = this.offsetWidth;

  var percentage = x / width;
  var random = Math.random(percentage);
  var imageNumber = Math.floor(random * images.length);

  if (
    event.pageX > pos.x + 100 ||
    event.pageY > pos.y + 100 ||
    event.pageY < pos.y - 100 ||
    event.pageX < pos.x - 100
  ) {
    $(images).each(function(image) {
      $(this).css("z-index", 0);
    });
    $(images)
      .eq(imageNumber)
      .css("z-index", 1);
  }
});
*,
html,
body {
  margin: 0;
}

.introImages {
  position: relative;
  width: 100vw;
  height: 100vh;
  display: block;
}

.introImages img {
  display: block;
  object-fit: cover;
  width: 100%;
  height: 100%;
  z-index: 0;
  position: absolute;
  left: 0;
  top: 0;
}

.intrImages:first-child {
  z-index: 1;
}
<div class="introImages">
	<img src="https://images.unsplash.com/photo-1563028327-1920b5a8d868?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=900&q=60">
  <img src="https://images.unsplash.com/photo-1556227703-ab57dbc6f839?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=900&q=60">
  <img src="https://images.unsplash.com/photo-1562960203-b4b41a48a1a1?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=900&q=60">
  <img src="https://images.unsplash.com/photo-1559501418-12357e4cf8fd?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=900&q=60">
  <img src="https://images.unsplash.com/photo-1561015477-1c907845c96b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=900&q=60">
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

您可以观看现场演示here

1 个答案:

答案 0 :(得分:0)

您可以设置transitions来延迟更改。延迟1秒的示例:

        var slideshow = $(".introImages");
var images = $(".introImages img");
var pos = { x: 0, y: 0 };

$(slideshow).on("mousemove", function(event) {
  var x = event.pageX;
  var width = this.offsetWidth;

  var percentage = x / width;
  var random = Math.random(percentage);
  var imageNumber = Math.floor(random * images.length);

  if (
    event.pageX > pos.x + 100 ||
    event.pageY > pos.y + 100 ||
    event.pageY < pos.y - 100 ||
    event.pageX < pos.x - 100
  ) {
    $(images).each(function(image) {
      $(this).css("z-index", 0);
    });
    $(images)
      .eq(imageNumber)
      .css("z-index", 1);
  }
});
*,html,body {
  margin: 0;
}

.introImages {
  position: relative;
  width: 100vw;
  height: 100vh;
  display: block;
}

.introImages img {
  display: block;
  object-fit: cover;
  width: 100%;
  height: 100%;
  z-index: 0;
  position: absolute;
  left: 0;
  top: 0;
  -webkit-transition: z-index 1s;
  transition: z-index 1s;  
}

.intrImages:first-child {
  z-index: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="introImages">
	<img src="https://images.unsplash.com/photo-1563028327-1920b5a8d868?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=900&q=60">
  <img src="https://images.unsplash.com/photo-1556227703-ab57dbc6f839?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=900&q=60">
  <img src="https://images.unsplash.com/photo-1562960203-b4b41a48a1a1?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=900&q=60">
  <img src="https://images.unsplash.com/photo-1559501418-12357e4cf8fd?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=900&q=60">
  <img src="https://images.unsplash.com/photo-1561015477-1c907845c96b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=900&q=60">
</div>