[英]CSS contain + position partially off-screen
I want to fit a background image, using the css background-size: contain;
我想使用css
background-size: contain;
适合背景图像background-size: contain;
properties, but position it partially off-screen. 属性,但将其部分置于屏幕外。 I have an image with a transparrent part and I would like to be able to manipulate the "fill", so I thought moving a background image into view would be a good way to achieve this:
我有一个具有透明部分的图像,并且希望能够操纵“填充”,所以我认为将背景图像移到视图中将是实现此目的的一种好方法:
.bottle {
background-image: res://bottle_fill;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: contain;
background-position: 50% 30%;
}
But it seems that with the contain parameter, the image can't "overspill" from the div? 但是似乎使用contain参数,图像不能从div“溢出”? I tried adding
overflow: hidden
but it didn't help. 我尝试添加
overflow: hidden
但没有帮助。 How can I achieve this? 我该如何实现? A JavaScript solution is fine if there isn't a css way.
如果没有CSS方式,则JavaScript解决方案很好。
https://jsfiddle.net/ghhxddnj/ here is a JSFiddle - try to get the smiley face half on, half off the screen, whilst it is sized contain
. https://jsfiddle.net/ghhxddnj/这里是一个的jsfiddle -试图让笑脸一半,一半关闭屏幕,而它的大小
contain
。 As noted by some of the comments, I know this is a slightly strange thing to want to do, but the background image must be the same size as the contents of the div (and <image>
sized with the contain parameter) the raw files are the same size, hence if they both use contain, they will come out the same size and I can set x align
to be center and then use y align
to control how much of the bottle is full. 正如一些评论所指出的,我知道这是一件很奇怪的事情,但是背景图像的大小必须与div文件的div内容的大小相同(且
<image>
大小包含contain参数)尺寸相同,因此如果它们都使用容器,它们的尺寸将相同,我可以将x align
设置为居中,然后使用y align
来控制瓶子的装满量。
Try this: 尝试这个:
var image_url = $('#logo').css('background-image'),
image;
// Remove url() or in case of Chrome url("")
image_url = image_url.match(/^url\("?(.+?)"?\)$/);
if (image_url[1]) {
image_url = image_url[1];
image = new Image();
// just in case it is not already loaded
image.src = image_url;
var imgoffset = image.height;
$('#logo').css('background-position',-imgoffset)
}
Source: How do I get background image size in jQuery? 资料来源: 如何在jQuery中获得背景图片大小?
EDIT: https://jsfiddle.net/ckf55axs/ 编辑: https : //jsfiddle.net/ckf55axs/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.