javascript - 在 mouseout 上保持视差移动一秒钟并平稳停止

标签 javascript jquery css mouse parallax

我想制作一个像本页一样具有鼠标视差效果的网站http://brightmedia.pl背景鼠标视差是如此平滑..

我有两个问题:

  1. 当您将鼠标悬停在某个容器上时,比方说,从左上角,图像会跳转。如何制作流畅的动画?

  2. 当鼠标移出容器时,如何让图像移动一点并以流畅的动画停止?

解决这些问题的代码是什么?

基本代码如下:

$('.container').mousemove( function(e){
    var xPos = e.pageX;
    var yPos = e.pageY;

  $('#par1').css({marginLeft: -xPos/20});

});
.container {
  position: relative;
  width: 100%;
  height: 800px;
  background: grey;
  overflow: hidden;
  margin: 0 auto;
}

.container img {
  width: 110%;
  height: 100vh;
  position: absolute;
}

body{
  height: 1000px;
}
h1{
  font-size: 60px;
  z-index: 10;
  position: absolute;
  left: 50%;
  top: 30%;
  transform: translate(-50%, -50%);
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" href="css.css">
  </head>
  <body>

<div class="container" id="container">
  <img id="par1" src="https://www.gettyimages.ca/gi-resources/images/Homepage/Hero/UK/CMS_Creative_164657191_Kingfisher.jpg" alt="">
  <h1>TEXT</h1>

</div>

    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="script.js"></script>
  </body>
</html>

最佳答案

因为我很久以前就解决了这个问题,所以我忘记了这篇文章,所以我决定更新答案。也许它会对其他人有所帮助。

使用 GSAP 解决了问题。下面你可以看到完全按照我想要的方式工作的代码

    let wrap = document.getElementById('container');
  	let request = null;
  	let mouse = { x: 0, y: 0 };
  	let cx = window.innerWidth / 2;
  	let cy = window.innerHeight / 2;
    
  	document.querySelector('.container').addEventListener('mousemove', function(event) {
  		mouse.x = event.pageX;
  		mouse.y = event.pageY;
      cancelAnimationFrame(request);
      request = requestAnimationFrame(update);
  	});
    
  	function update() {
  		dx = mouse.x - cx;
  		dy = mouse.y - cy;
  		let tiltx = (dy / cy );
  		let tilty = - (dx / cx);

      TweenMax.to("#container img", 1, {x:-tilty*20, y:-tiltx*20, rotation:0.01, ease:Power2.easeOut});
    }
    
    window.addEventListener('resize', function(event){
       window.innerWidth / 2;
    	 window.innerHeight / 2;
    });
* {
  margin:0;
  padding:0;
  box-sizing:border-box;
}
.container {
  position: relative;
  width: 100%;
  height: 100vh;
  overflow: hidden;
  display:flex;
  flex-direction:column;
  justify-content:center;
  align-items:center;
}
.container img {
  width: 110%;
  height: 120vh;
  position: absolute;
}
h1 {
  z-index:100;
  font-size: 6rem;
  z-index: 10;
  color:#333;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/latest/TweenMax.min.js"></script>
<div class="container" id="container">
  <img id="par1" src="https://www.gettyimages.ca/gi-resources/images/Homepage/Hero/UK/CMS_Creative_164657191_Kingfisher.jpg" alt="">
  <h1>GSAP Mouse Parallax</h1>

</div>

关于javascript - 在 mouseout 上保持视差移动一秒钟并平稳停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49072182/

相关文章:

javascript - Mongo DB 在数组中返回未定义的值

jquery - 对于具有自动高度和自定义上一个/下一个导航的简单 jquery slider 有什么建议吗?

javascript - 使用 HTML5 拖放

javascript - jQuery 通过单击页面上的任意位置关闭 DIV

javascript - 在窗口的右下角放置一个元素(不是视口(viewport))

javascript - 如何在不使用 HTML5 中的输入文件的情况下上传在 java 中创建的文件

javascript - 使用 JavaScript 在其他图像之上添加透明图像

html - 如何使用 float 属性将图像水平居中?

c# - 在 javascript 中读取 JavaScriptSerializer c#

html - 如何去除div周围的蓝色边框