简体   繁体   English

如何在背景大小的div上缩放背景图像

[英]how do I zoom a background image on a div with background-size

I want a div element to stretch across that is 33% width with a background image done in css 我想要一个div元素伸展到33%宽度,背景图像用css完成

background-image:url(); background-size:cover 

How do I animate a zoom-in of the image of the background in the div on mouseover or mouseneter, is there a plugin that can do this? 如何在鼠标悬停或鼠标中对div中背景图像的放大设置动画,是否有插件可以执行此操作? The background div has to use background-size:cover because it's an elastic page. 背景div必须使用background-size:cover,因为它是一个弹性页面。

I don't have a fiddle yet cos I don't know where or how to start 我没有小提琴,但我不知道在哪里或如何开始

Answer for those who wants to hack CSS transitioning zooming to get applied on background-size: cover; 那些想要破解CSS转换缩放以应用于背景大小的人的答案:封面;

-- if not, than read the second section for standard ways to achieve such effect - 如果没有,请阅读第二部分,了解实现此类效果的标准方法

<div class="wrap">
    <div></div>
    <p>hello</p>
</div>


html, body {
    height: 100%;
}

div.wrap {
    height: 33%;
    width: 33%;
    overflow: hidden;
    position: relative;
}

div.wrap > div {
    position: absolute;
    height: 100%;
    width: 100%;
    -moz-transition: all .5s;
    -webkit-transition: all .5s;
    transition: all .5s;
    -moz-transform: scale(1,1);
    -webkit-transform: scale(1,1);
    transform: scale(1,1);
    background-image: url('http://pimg.tradeindia.com/00288122/b/0/Our-Valuable-Client-List-Click-on-Image-.jpg');
    -moz-background-size: cover;
    -webkit-background-size: cover;
    background-size: cover;
    z-index: -1;
}

div.wrap:hover > div {
    -moz-transform: scale(2,2);
    -webkit-transform: scale(2,2);
    transform: scale(2,2);    
}

Demo (Using background-size: cover; to transition/zoom the element) 演示 (使用background-size: cover;转换/缩放元素)

As you said that transitioning the cover size is necessary, so I came up with the trick which I had told you previously, here I have a child element nested under position: relative; 正如你所说过,转换cover大小是必要的,所以我想出了我之前告诉你的技巧,这里我有一个嵌套在position: relative;下的子元素position: relative; element where am having the child element set to position: absolute; 将子元素设置为position: absolute;元素position: absolute; with background-image having background-size set to cover and then on hover of parent, I zoom in the element using the transform: scale(2,2); background-image background-size设置为cover ,然后在父母的hover ,我使用transform: scale(2,2);放大元素transform: scale(2,2); property. 属性。

Also, a crucial thing while working with this solution is that we need to set the z-index of the position: absolute; 此外,使用此解决方案时至关重要的是我们需要设置位置的z-index position: absolute; element lower than what the elements you will be placing inside, so it will act like a background 元素低于你将放置的元素,因此它将像background


Answer for those who want to go clean with HTML and CSS 那些想要使用HTML和CSS清理的人的答案

You cannot animate a background-size if it's value is cover so either you will need px or % , or you can also use an img tag with transform: scale(2,2); 如果它的值是cover ,则无法为background-size设置动画,因此您需要px% ,或者您也可以使用带有transform: scale(2,2);img标记transform: scale(2,2); property. 属性。

Demo 演示

Demo 2 (zoom-in or zoom-out from the center) 演示2 (从中心放大或缩小)

div {
    height: 200px;
    width: 200px;
    background: url('http://pimg.tradeindia.com/00288122/b/0/Our-Valuable-Client-List-Click-on-Image-.jpg');
    background-size: 100% 100%;
    -moz-transition: all .5s;
    -webkit-transition: all .5s;
    transition: all .5s;
}

div:hover {
    background-size: 150% 150%;
}

If you want to stick with background-size: cover; 如果你想坚持background-size: cover; than you will have to wrap entire element inside a wrapper element having fixed dimensions with overflow: hidden; 你将不得不将整个元素包装在一个具有固定尺寸的包装元素中,并使用overflow: hidden; and than scale the child element on hover of parent element. 而且在父元素hover时缩放子元素。


As you commented, for an img tag example, you can use transform: scale(2,2); 正如您所评论的,对于img标记示例,您可以使用transform: scale(2,2); to achieve that with the parent element set to overflow: hidden; 实现那个父元素设置为overflow: hidden;

Demo 2 演示2

div {
    height:300px;
    width: 300px;
    overflow: hidden;
}

div img {
    -moz-transition: all .5s;
    -webkit-transition: all .5s;
    transition: all .5s;
    -moz-transform: scale(1,1);
    -webkit-transform: scale(1,1);
    transform: scale(1,1);
}

div:hover img {
    -moz-transform: scale(2,2);
    -webkit-transform: scale(2,2);
    transform: scale(2,2);
}
<script>
    function animate(){
        document.getElementById('a').style.webkitTransitionDuration='1s';
        document.getElementById('a').style.backgroundSize="200% 200%";
    }
</script>
<div id="a" onmouseover="animate()" style="width: 200px; height: 70px; border: 1px solid; background: url('http://www.wpclipart.com/food/fruit/apple/apples_4/apple_honeycrisp_small.png') no-repeat;background-size: 100% 100%; ">
</div>

You can do something like this for webkit browsers. 您可以为webkit浏览器执行类似的操作。

Demo Here 在这里演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用mootools Fx缩放背景大小 - Use mootools Fx to zoom background-size 使用 background-size:cover 后如何更改 javascript 中的背景 url? - How do I change the background url in javascript after using background-size: cover? 如何基于图像大小在CSS中添加背景大小? - How to add background-size in css based on image size? 如何在具有特定百分比的父 div 中显示带有背景大小封面的图像 - How to display an image with background-size cover in a parent div with specific percentage 使用“background-size : 100% 100%”时,DIV“background-image”没有在整个DIV上拉伸 - DIV "background-image" not stretched over entire DIV when using "background-size : 100% 100%" 如何设置多个背景的背景大小的动画 - How to animate background-size of multiple background 使用background-size:contains时,获取XY背景和背景图像的高度/宽度 - Get XY coords & Height/Width of div background image, when using background-size:contain 如何与background-size:cover一样调整图像大小/裁剪图像? - How do I resize/crop images simualar to how background-size:cover does? 如何应用“background-size:cover”所具有的相同效果<img> - How do I apply the same effect that “background-size: cover” does, on a <img> 当背景尺寸被覆盖时,如何获得当前的“真实”背景图像尺寸? - How to get the current, “real”, background image size, when background-size is cover?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM