gpt4 book ai didi

javascript - 滚动负责的背景图像覆盖尺寸 : is it possible?

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

所以我有这样的挑战: body 背景上的图像,背景大小,覆盖以适合整个屏幕。在背景图像上是一些元素(建筑物)。所以我想将鼠标悬停在建筑物上,他们会更改颜色或添加阴影等。问题在于屏幕调整大小,当我调整屏幕大小时我找不到一个公式可以移动覆盖 div 以适应建筑物背景。

html:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>website</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<link rel="stylesheet" type="text/css" href="css/style.css">

<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="js/modernizr.js"></script>
<script type="text/javascript" src="js/fixes.js"></script>

<!--[if lt IE 9]>
<script type="text/javascript" src="js/css3-mediaqueries.js"></script>
<![endif]-->
</head>

<body class="home">
<div class="map">
<div class="building1"></div>
</div>
</body>
</html>

CSS:

.home{
background:url(../img/homeBg1200.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}

js:

$(document).ready(function() {
function fixes() {
var windowW = window.innerWidth,
windowH = window.innerHeight;

if(windowW > 1199) {
var proportionH,
proportionW,
indexTop,
indexLeft,
top1,
left1,
width1,
height1;



//indexTop = (proportionH-800)/3;

if((windowW / windowH) < 1.5) {
proportionW = (windowH * 1200/800);
proportionH = (proportionW * 800/1200);
indexLeft = (proportionW - 1200)*0.001;
indexTop = (proportionH - 800)*0.001;
top1 = proportionH*0.178-indexTop;
left1 = proportionW*0.476-indexLeft;
width1 = (proportionW )*0.084;
height1 = (proportionH)*0.482;
} else {
proportionH = (windowW * 800/1200);
proportionW = (proportionH * 1200/800);
indexLeft = (proportionW - 1200)*0.001;
indexTop = (proportionH - 800)/2;
top1 = proportionH*0.178-indexTop;
left1 = proportionW*0.476+indexLeft;
width1 = (proportionW )*0.084;
height1 = (proportionH)*0.482;
}

$('.building1').css({
'top': top1,
'left': left1,
'width':width1,
'height':height1
});

}
}

fixes();

$(window).resize(function() {
fixes();
});
});

这有可能实现吗?

谢谢

ps:刚刚复制了这里的代码:http://hardware-trade.com/site/

最佳答案

这可以不用 javascript 来解决。你应该在 css 中使用媒体查询。

关于javascript - 滚动负责的背景图像覆盖尺寸 : is it possible?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18636689/

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