在分区内使用后拉伸并将宽度减小到分区的宽度
我正在尝试在部门内使用 backstretch 。我已经弄清楚如何做到这一点此处,但图像的宽度仍然拉伸到充满整个身体。有没有办法控制后拉伸图像,使其填充分区的高度和宽度,而不是整个身体? (划分是流动的,否则我会使用静态背景图像。)
I'm attempting to use backstretch inside a division. I've figured out how to do that here but the width of the image is still stretching to fill the entire body. Is there a way to control the backstretch image so that it fills the height and width of the division and not the entire body? (The division is fluid, or otherwise I'd use a static background image.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
*编辑:自从我写这篇文章以来,我已经停止在正常设置下使用后拉伸,因为它似乎在 Android 智能手机上或平板电脑方向改变时根本无法正常工作。然而,这里解释的这种修改仍然有效,所以如果你想要一个固定高度的版本,请继续阅读 - 我已附上下面的 JS,HTML 是;
<正文>
这是 JS 文件的链接 -> http://www.mcnab.co/backstretch.js
我想使用 backstretch 来填充元素是全宽的,但只占据了屏幕顶部的 560px。如果图像的大小必须调整为大于原始图像,它不会垂直居中,而是会偏移,因此顶部消失。
是的,上面的链接是正确的,您将容器从主体更改为要填充的元素,在本例中是#backimage
然后我更改了代码以将固定高度的CSS分配给加载的图像而不是100%;
为了实现这一点,_adjustBG 方法必须进行相当多的更改。首先,您需要为 imgWidth 和 imgHeight 变量提供全局范围。从中删除它们
并将它们添加到
最后,我重写了 _adjustBG 方法的一部分以使用 imgWidth 和 imgHeight,而不是根据检测到的浏览器宽度和高度来获取它们。
};
它对我很有用,我希望这对其他人有帮助。如果有人来到这里并对我如何做得更好有任何意见,那么我很高兴听到他们的意见。
干杯,
罗比。
*Edit: Since I wrote this I've stopped using backstretch on normal settings because it doesn't seem to work at all well on Android smartphones or when tablets have their orientation changed. However, this modification explained here still works great, so if you want a fixed height version read on- I've attached the JS below, the HTML is;
<body>
<div id="backimage"></div>
</body>
Here's a link to the JS file -> http://www.mcnab.co/backstretch.js
I wanted to use backstretch to fill an element which was full width but took up only the top 560px of the screen. And if the image had to be resized larger than the original it didn't recenter vertically but was offset so the top disappeared.
Yes, the link above is correct, you change the container from the body to the element you want to fill, in this case it was #backimage
Then I changed the code to assign a fixed height css to the image loaded rather than 100%;
To achieve this the _adjustBG method has to be changed quite a bit. First of all you need to give the imgWidth and imgHeight variables a global scope. Remove them from
and add them to
Finally I rewrote a chunk of the _adjustBG method to use the imgWidth and imgHeight rather than have it all basedon the detected browser width and height.
};
It's working great for me, and I hope this helps somebody else. If anyone comes by here and has any comments on how I could have done it better then I'd be happy to hear them.
Cheers,
Robbie.
我这样做就简单多了。在 backstretch.js 中查找
ratio
。它说的是这样的:在尝试了一百万个不同的公式之后,我将其更改为 h/h-1。然后我意识到这实际上是一个零比率。因此将其更改为:
现在,无论高度如何,图像都保持 100% 宽度。然后,我使用 @media 标签在 css 中设置一些最大宽度和最小宽度,并确保页面的内容部分始终向上跳跃,以确保图像变小时图像下方没有空白。您可以在此处查看示例:
http://katjaglieson.com
这花了我一整天的时间,哈哈。
但本质上,这阻止了向后伸展的疯狂全屏,但保留了所有其他好处。
:)
I did this much simplier. Look for the
ratio
in the backstretch.js. It says something like:I changed it to h/h-1 - after trying a million different formulas. Then I realized this is actually a ratio of zero. So change it to:
Now the images are sticking at 100% width regardless of the height. I then set some max-width and min-width in css with the @media tag and made sure the content section of the page was always jumping up to ensure no whitespace below the image as it gets smaller. You can see the example here:
http://katjaglieson.com
This took me a whole day lol.
But essentially this is stopping the crazy full screen-ness of backstretch but keeping all the other goodies.
:)