gpt4 book ai didi

jquery - 图像悬停移动导致 X 轴溢出?

转载 作者:行者123 更新时间:2023-11-28 03:52:39 25 4
gpt4 key购买 nike

作为我之前问题的后续问题 here ,我试图在页面的两侧制作悬停图像。

jsfiddle

JS:

var movementStrength = 25;
var w = $(window).width();
var h = $(window).height();

$(window).mousemove(function(e){
var pageX = (e.pageX - w / 2) / w / 2;
var pageY = (e.pageY - h / 2) / h / 2;
var newvalueX = pageX * movementStrength;
var newvalueY = pageY * movementStrength;
$('.top-image-left').css({ left: newvalueX + 'px', top: newvalueY + 'px'});
});


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class='top-contain-left'>
<div class="top-image-left">
</div>
</div>

<div class='top-contain-right'>
<div class="top-image-right"></div>
</div>

HTML:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class='top-contain-left'>
<div class="top-image-left">
</div>
</div>

<div class='top-contain-right'>
<div class="top-image-right"></div>
</div>

CSS:

.top-contain-left {
padding: 25px;
width: 35%;
height: 35%;
position: absolute;
top: 400px;
}

.top-image-left {
background: url('http://i.imgur.com/wZRaMrB.png');
position: absolute;
background-size: contain;
width: 100%;
z-index: 0;
height: 100%;
background-repeat: no-repeat;
}


.top-contain-right {
padding:25px;
width:35%;
height:35%;
position:absolute;
top:400px;
right: -20%;
}


.top-image-right {
background:url('http://i.imgur.com/Qn6xkCZ.png');
position:absolute ;
background-size: contain;
width:100%;
z-index:0;
height:100%;
background-repeat: no-repeat;
}

您会注意到右侧有溢出。 (此时不能上传超过2个链接)

也可以在我的网站上查看http://jenngaudio.x10host.com/Flower%20Spark/

我已经尝试过 overflow-x: hidden 属性,但它会导致整个页面出现问题 - 可能是因为我使用的是响应式框架。

最佳答案

右填充是由默认的 background-position 引起的,它是 left top。图像的宽度小于容器 div 的宽度,因此您会看到正确的填充。要解决此问题,只需使用 background-position: right top 将背景图像右对齐。您还应该将 overflow-x:hidden 应用到 body 以防止水平滚动条在移动鼠标时出现和消失。最后,我稍微修复了您的脚本,使其仅使用 1 个 window.onmousemove 处理程序。您不需要 2 个处理程序,因为您使用相同的计算值来更新 2 个图像的位置(即使您需要不同的值,也只是在同一个处理程序中执行不同的计算)。

这是更新后的代码:

var movementStrength = 25;
var w = $(window).width();
var h = $(window).height();

$(window).mousemove(function(e){
var pageX = (e.pageX - w / 2) / w / 2;
var pageY = (e.pageY - h / 2) / h / 2;
var newvalueX = pageX * movementStrength;
var newvalueY = pageY * movementStrength;
$('.top-image-left').css({ left: newvalueX + 'px', top: newvalueY + 'px'});
$('.top-image-right').css({ right: newvalueX + 'px', top: newvalueY + 'px'});
});
.top-contain-left {
padding: 25px;
width: 35%;
height: 35%;
position: absolute;
top: 400px;
}

.top-image-left {
background: url('http://i.imgur.com/wZRaMrB.png');
position: absolute;
background-size: contain;
width: 100%;
z-index: 0;
height: 100%;
background-repeat: no-repeat;
}


.top-contain-right {
padding:25px;
width:35%;
height:35%;
position:absolute;
top:400px;
right: 0%;
}


.top-image-right {
background:url('http://i.imgur.com/Qn6xkCZ.png') right top;
position:absolute ;
background-size: contain;
width:100%;
z-index:0;
height:100%;
background-repeat: no-repeat;
right:0px;
}
body {
overflow-x:hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class='top-contain-left'>
<div class="top-image-left">
</div>
</div>

<div class='top-contain-right'>
<div class="top-image-right"></div>
</div>

关于jquery - 图像悬停移动导致 X 轴溢出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43646593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com