【发布时间】:2018-06-19 05:03:21
【问题描述】:
我正在尝试根据用户浏览器窗口的大小调整我的 Square 空间网站(链接 here)上的背景图像大小。更具体地说,如果用户的窗口大小低于某个宽度,则图像会调整为更小的尺寸并保持其纵横比。
这似乎是一个常见问题。我已经挖掘了几个小时,似乎无法找到解决方案。我正在使用 JavaScript 来完成这项工作,但它无法完成。
请注意,我说的是与流体调整大小不同的东西(即使用百分比值)。
我已通过质量验证运行此代码,它返回时没有出现语法错误。所以除此之外,我迷路了。
我将这个 (jQuery) 脚本注入到页眉中:
$(document).ready(function() {
function imageresize() {
var contentwidth = $(window).width();
if ((contentwidth) < 1500) {
$(".widescreen").attr("height", "480px");
} else {
$(".widescreen").attr("height", "768px");
}
}
imageresize(); //Triggers when document first loads
$(window).on("resize", function() {
imageresize();
});
});
.widescreen {
background: url(https://i.imgur.com/96pxGnd.jpg) center top no-repeat;
background-size: cover;
width: 100vw;
position: relative;
left: 50%;
right: 50%;
margin-left: -50vw;
margin-right: -50vw;
height: 768px;
/* Trying to change this value so the rest of the CSS adjusts accordingly */
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
语法错了吗?请帮忙!并提前感谢您。
【问题讨论】:
-
如果它有效,为什么你不想使用百分比值?
-
出于几个原因:1. 保持设计的刚性 2. 所以我知道如何针对不同设备更改设计
标签: javascript jquery html css