简体   繁体   English

当 div 悬停时扩展图像或背景

[英]Expand an image or background when div is hovered

I need a div background image to expand when the div is hovered.当 div 悬停时,我需要一个 div 背景图像来展开。 What would the best way to do this be?最好的方法是什么?

It needs to be a smooth animation.它需要是一个流畅的动画。 Ideally I would like to use JQuery but I am very new to coding so is there a ready made code I could use?理想情况下,我想使用 JQuery,但我对编码很陌生,所以有我可以使用的现成代码吗?

I Have just created this code and IT WORKS A TREAT!我刚刚创建了这个代码,它很有效! I hope it helps我希望它有帮助

<img class="wooll" src="/images1" style="position: absolute; margin: 0 0 0 20px;" />

<img  style="" src="/images2" class="grow"/>


$(".wooll, .grow").hover(function() {
   $('.grow').animate({
        'height': $(this).height() * 1.4,
        'width': $(this).width() * 1.4
    });
}, function() {
    $('wool, .grow').animate({
        'height': $(this).height() / 1,
        'width': $(this).width() / 1
    });
});​

Here is a very basic example of the syntax and a working example .这是一个非常基本的语法示例和一个工作示例

$("#mydiv").mouseover(function(){
    $("#mydiv").animate({width: '200', height: '200'},200);
});

I would highly recommend reading about the animate function in the JQuery API and exploring the features of the framework.我强烈建议阅读 JQuery API 中的animate 函数并探索该框架的特性。

Very simple way of doing it!很简单的做法! You can use CSS3 for the smooth transition, just apply this to the background in css:您可以使用 CSS3 进行平滑过渡,只需将其应用于 css 中的背景:

-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;

Then for the hover然后对于悬停

div:hover .background {
background-size: cover /* or you can use width/height */
}

Working example on JSFiddle: http://jsfiddle.net/AVyQc/3/ JSFiddle 的工作示例: http : //jsfiddle.net/AVyQc/3/

With CSS3 , you can add an effect when changing from one style to another, without using Flash animations or JavaScripts and with use of only pure css .使用CSS3 ,您可以在从一种样式更改为另一种样式时添加效果,而无需使用 Flash 动画或 JavaScript 并且仅使用纯 css

see the demo :- DEMO1看演示:- DEMO1

see the demo :- DEMO2看演示:- DEMO2

I hope this will help you我希望这能帮到您

HTML HTML

<div>
</div>

CSS CSS

div {
width:100px;
height:100px;
background:red;
}
div:hover {
width:200px;
height:200px;
background:yellow;
  -webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
}

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

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