调整可拖动对象背景图像的大小
This question is related to : Resizable, draggable object in jquery. Possible?
Hello all,
I want to resize the background image of a div on jquery resize. How can I do it?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法设置 CSS 背景图像的大小。但也许这个 http://www.cssplay.co.uk/layouts/background.html< /a> 或者这会有所帮助 http://css-tricks.com/how -to-resizeable-background-image/
You can't set the size of a CSS background-image. But maybe this http://www.cssplay.co.uk/layouts/background.html or this will help http://css-tricks.com/how-to-resizeable-background-image/
基本上,这将由一个可调整大小的容器(我说 div)和一个非静态位置(如果你使其可拖动,这将不可避免地发生)、一个位置设置为绝对和 100% 宽度的“背景”对象组成/height,以及设置为相对位置的内容容器(另一个 div)。应该做到这一点。
查看示例: http://jsfiddle.net/wVAT2/
将图像的位置设置为Absolute 将其从文档流中取出,并将其相对于第一个非静态定位的祖先(在本例中为包含的可拖动 div)定位。内容 div 上的相对位置是为了确保图像后面的内容正确的堆栈顺序,否则“背景”将成为前景。
如果您想使用该特定标记来支持 IE7,以修复 jQuery UI 大小调整控件的堆叠上下文,您可能需要考虑一下,但我将把这个问题留给您。希望这将为您指明正确的方向。
如果您想保持背景纵横比,而不是在调整大小时拉伸背景,请考虑绑定一个在可调整大小的调整大小事件上调整图像元素大小的函数。
Basically this would consist of a resizable container (I'd say div) with a non-static position (that'll happen inevitably if you're making it draggable), a "background" object with position set to absolute and 100% width/height, and a content container (another div) set to position relative. Should do the trick.
Check out an example: http://jsfiddle.net/wVAT2/
What happens is that setting the image's position to absolute takes it out of the document flow and positions it relative to the first non-statically positioned ancestor (in this case, the containing draggable div). The relative position on the content div is to ensure the proper stack order for the content that follows the image, otherwise the "background" becomes the foreground.
You'd probably have to do some thinking if you wanted to support IE7 with that particular markup to fix the stacking context for jQuery UI's resizing controls, but I'll leave that one to you. Hopefully this will point you in the right direction.
If you want to maintain the background aspect ratio rather than having it stretch as you resize, consider binding a function that resizes the image element on the resizable's resize event.