简体   繁体   English

鼠标悬停时缩放带有图像的div

[英]Scale a div with an image in it on mouseover

I need to keep the image element (.myImages) within the div (.Holder). 我需要将图像元素(.myImages)保留在div(.Holder)中。 On page load, it is sitting bang in the centre of the div. 在页面加载时,它位于div中心。 On mouseOver of the image (.myImages), the image zooms to a scale of "2", and the container div animates to become larger in both width and height. 在图像(.myImages)的mouseOver上,图像缩放到“ 2”的比例,并且容器div设置动画以使其宽度和高度都变大。 Problem is that when the image scales on hover, it comes right out of the top of the div. 问题是,当图像在悬停时缩放时,它恰好位于div的顶部。 I want it to expand within the div. 我希望它在div内扩展。 Not to go outside it. 不要去外面。 All answers appreciated. 所有答案表示赞赏。

Code: 码:

 $(document).ready(function(){ $('.myImages').hover(function() { $(this).addClass('transition'); $('.Holder').animate({ width: '600', height: '410' }); }, function() { $(this).removeClass('transition'); $('.Holder').animate({ width: '300', height: '250' }); }); }); 
 .myImages { -webkit-transition: all .2s ease-in-out; -moz-transition: all .2s ease-in-out; -o-transition: all .2s ease-in-out; -ms-transition: all .2s ease-in-out; z-index:-1; margin:20px; } .transition { -webkit-transform: scale(2); -moz-transform: scale(2); -o-transform: scale(2); transform: scale(2); } .Holder{position: relative; background-color:white; text-align: center; width: 300px; height: 250px; border: 2px solid; margin:200px; padding:0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class ="Holder"> <img class="myImages" onclick="changeImage()" src="a.jpg" width="200" height="180"> </div> 

Here's a solution that uses the CSS hover pseudoclass to double the size of the div when it's moused over. 这是一个使用CSS 悬停伪类将div的大小翻倍的解决方案。 Is this close to what you were looking for? 这与您想要的东西接近吗?

 .myImages { z-index: -1; margin: 20px; } .transition { -webkit-transform: scale(2); -moz-transform: scale(2); -o-transform: scale(2); transform: scale(2); } .Holder { position: relative; background-color: white; text-align: center; width: 300px; height: 250px; border: 2px solid; margin: 20px; padding: 0; -webkit-transition: all .2s ease-in-out; -moz-transition: all .2s ease-in-out; -o-transition: all .2s ease-in-out; -ms-transition: all .2s ease-in-out; } .Holder:hover { transform-origin: top left; transform: scale(2.0); } 
 <div class ="Holder"> <img class="myImages" src="http://i.imgur.com/8mro6SN.jpg?1" width="200" height="180"> </div> 

CodePen: http://codepen.io/maxlaumeister/pen/LVayOO CodePen: http ://codepen.io/maxlaumeister/pen/LVayOO

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM