- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用backstretch一个部门内部。我已经弄清楚如何做到这一点here但图像的宽度仍在拉伸(stretch)以填充整个 body 。有没有办法控制后拉伸(stretch)图像,使其填充分区的高度和宽度,而不是整个 body ? (划分是流动的,否则我会使用静态背景图像。)
最佳答案
*编辑:自从我写这篇文章以来,我已经停止在正常设置下使用后拉伸(stretch),因为它似乎在 Android 智能手机上或平板电脑方向改变时根本无法正常工作。然而,这里解释的这个修改仍然很好用,所以如果你想要一个固定高度的版本,请继续阅读 - 我已经附上了下面的 JS,HTML 是;
<body>
<div id="backimage"></div>
</body>
这是 JS 文件的链接 -> http://www.mcnab.co/backstretch.js
我想使用 backstretch 来填充一个全宽但只占据屏幕顶部 560px 的元素。如果图像的大小必须调整为大于原始图像,它不会垂直居中,而是会偏移,因此顶部消失。
是的,上面的链接是正确的,您将容器从正文更改为要填充的元素,在本例中为#backimage
// Append the container to the #backimage, if it's not already there
if($("#backimage #backstretch").length == 0) {
$("#backimage").append(container);
}
然后我更改了代码,为加载的图像分配固定高度的 css,而不是 100%;
// If this is the first time that backstretch is being called
if(container.length == 0) {
container = $("<div />").attr("id", "backstretch")
.css({left: 0, top: 0, position: "fixed", overflow: "hidden", zIndex: -999999, margin: 0, padding: 0, height: "560", width: "100%"});
} else {
// Prepare to delete any old images
container.find("img").addClass("deleteable");
}
为了实现这一点,_adjustBG 方法必须进行相当多的更改。首先,您需要为 imgWidth 和 imgHeight 变量提供全局范围。将它们从
中删除var self = $(this);
并将它们添加到
rootElement = ("onorientationchange" in window) ? $(document) : $(window),
imgRatio, bgImg, imgWidth, imgHeight, bgWidth, bgHeight, bgOffset, bgCSS;
最后,我重写了 _adjustBG 方法的一部分以使用 imgWidth 和 imgHeight,而不是全部基于检测到的浏览器宽度和高度。
function _adjustBG(fn) {
try {
bgCSS = {left: 0, top: 0}
bgWidth = rootElement.width();
bgHeight = bgWidth / imgRatio;
if( bgHeight <= 560 ) {
bgHeight = 560;
bgWidth = 560 * imgRatio;
bgOffset = ( bgWidth - rootElement.width() ) / 2;
if(settings.centeredY) $.extend(bgCSS, {left: "-" + bgOffset + "px"});
} else {
bgHeight = (rootElement.width() / imgRatio);
bgWidth = rootElement.width()
bgOffset = ( bgHeight - 560 );
if(settings.centeredY) $.extend(bgCSS, {top: "-" + bgOffset + "px"});
}
$("#backstretch, #backstretch img:last").width( bgWidth ).height( bgHeight )
.filter("img").css(bgCSS);
} catch(err) {
// IE7 seems to trigger _adjustBG before the image is loaded.
// This try/catch block is a hack to let it fail gracefully.
}
// Executed the passed in function, if necessary
if (typeof fn == "function") fn();
}
};
这对我很有用,我希望这对其他人有帮助。如果有人来到这里并对我如何做得更好有任何意见,那么我很乐意听到他们的意见。
干杯,
罗比。
关于jquery - 在分区内使用后拉伸(stretch)并将宽度减小到分区的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7125499/
我想创建一个随页面宽度变化的页眉。 我的标题由三部分组成,固定宽度的两侧和可拉伸(stretch)的中间部分。 我使用 displa
我正在尝试在导航栏下方创建一列内容,以便内容的宽度与导航栏的宽度完全匹配。事实证明这比我预期的要困难。 给定这样的东西: link
我有一个大小为 12*30 的字符串的图像。我想创建一个动画,让它给人一种拉伸(stretch)字符串的感觉。我通过缩放图像来做到这一点,但我面临的问题是缩放图像没有发生碰撞。它仅出现在原始图像大小的
在 Silverlight 中,如何拉伸(stretch) Line 的宽度以填充作为子元素的 StackPanel 的宽度?更喜欢 XAML 解决方案,而不是代码隐藏。这是我在 WPF 中的操作方法
QML 怎么可能?自动拉伸(stretch)元素以使其所有子元素都适合它?以及如何指定间距?例如,我想在文本周围有一个矩形。矩形应该有一些内部间距。 如果我写以下内容,则矩形的大小为 0,0。 Rec
我已经习惯了使用 matlab,现在正在尝试转换 matplotlib 和 numpy。 matplotlib 中是否有一种方法可以让您绘制的图像占据整个图形窗口。 import numpy as n
我有一个(简化的)2x2 网格,里面有三个控件。左控制在两行上延伸。
假设我有 3 个大小不匹配的列表,[3, 7, 6]、[12, 67, 89, 98] 和 [1, 2, 3, 4, 5, 6, 7]我想要一个函数来执行此操作: >>> stretch([3, 7,
如果 Image 被 Stretched,我可以获取 Height 和 Width 吗>UniformToFill? 我尝试了 Width 和 Height 属性,但它们始终是 NaN。 最佳答案 如
当我为微调器使用我自己的背景主题时,它是拉伸(stretch)图标。 我的微调器代码是: 我的 spinner.xml 是: 和我从 sdk 4 复制的图
我正在尝试在 OpenGL 中创建一个简单的 GUI。我创建了一张图片,以便我可以引用它并使解释更简单: 当我将按钮 (32x32) 的纹理应用到大小为 120x20 的四边形多边形(即不是矩形作为纹
SurfaceTexture 预览被拉伸(stretch)了!我正在开发一个 camera2 应用程序。每个设备都工作不好。视频和照片的预览被拉伸(stretch)。 public class Cam
我使用一些ImageButtons 创建了一个Android layout。我的布局在小屏幕(例如 3.6 英寸到 4.2 英寸)上运行良好,但是当我在 7 英寸或 9 英寸平板电脑上使用我的应用程序
我在拉伸(stretch) GridView 以适应显示宽度时遇到问题。任何人都知道如何解决这个问题?我希望 GridView 占据整个宽度,包括蓝色标记的字段(见下图)
我使用位图作为 Activity 的背景。自定义背景代码为: 但是它位于中间,并没有占据整个屏幕空间。我怎样才能让它填满所有可用空间?我尝试将宽度和高度设置为 fill_parent。 谢谢 最
所以我最近一直在为 iOS 开发应用程序,但遇到了一个难题。我正在制作一个迷你合成器,然后开始制作 GUI。我开始制作键盘(目前为 2 个 Octave )并测量了白键的大致长度。一切都很好,直到我在
好的,所以我在 slider 内有一个图像,但它不能正确缩放。 先小后伸,想一直伸到底,怎么实现? 看一下截图(忽略第二张截图中的红色部分): 还有CSS: .slider-wrapper {
我有下面的 HTML,我希望容器 div 拉伸(stretch)到窗口的整个高度。这对 wine 有效,但是当您移除容器内的两个蓝色 div 时会发生什么?容器延伸到其中蓝色 div 的底部,但不会延
我正尝试在 this example 之后制作背景,但我需要嵌套 2 个容器。 我的代码看起来像这样: XHTML:
这是关于用颜色填充背景。 我正在构建 this project 的 SwiftUI 版本.这是一个选项卡式应用程序(测试工具)。 This is my Working Project 我要做的第一件事
我是一名优秀的程序员,十分优秀!