Question

I need to reduce the image size using jquery(not the div size) , when I entered the mouse into the div which contains an image.

My div will be,

<div class="toolbarIcon" >
    <img width="40px" height="40px" src="toolbar/user_login.png"/><label class="toolbarLabel">Login</label>
</div>

CSS will be,

.toolbarIcon {
    text-align: center;
    border-style: solid;
    border-width: 1px;
    border-color: #E6E6E6;

    width: 60px;
    float: left;
}

Also jquery will be,

$(document).ready(function(){

    $("#toolbar").corner("5px");
    $(".toolbarIcon").corner();

    $(".toolbarIcon").mouseover(function(){
        $(this).css("background-color","#FFCC80");
    });
    $(".toolbarIcon").mouseout(function(){
        $(this).css("background-color","#EBEBFF");
    });


    $(".toolbarIcon").mouseup(function(){
        $(this).css("background-color","#FFCC80");
    });
    $(".toolbarIcon").mousedown(function(){
        $(this).css("background-color","#E6B85C");

    });

});

The design from,

enter image description here

To ,

enter image description here

Note : The size of the image was changed.How can I achieve this using Jquery , When I entered the mouse ion the div.

Good answers are definitely appreciated.

Was it helpful?

Solution

You can just set the size of the image and the browser will scale it for you and I would recommend using the .hover() event which covers both mouse over and mouse leave:

$(".toolbarIcon").hover(function(){
    $(this).css("background-color","#FFCC80");
    $(this).find("img").css({height: "30px", width: "30px"});
}, function() {
    $(this).css("background-color","#EBEBFF");
    $(this).find("img").css({height: "40px", width: "40px"})
});

You could also use just CSS for this too:

.toolbarIcon:hover img {
     width: 30px;
     height: 30px;
}

Depending upon the exact effect you want, you may also want to tweak the padding below the image to keep it vertically centered when you hover.

OTHER TIPS

CSS only:

LIVE DEMO

.toolbarIcon:hover img{
  width:35px;
  height:35px;
  padding-bottom:5px; // compensate resize
}

DEMO WITH CSS3 ANIMATION

.toolbarIcon img{
  padding-bottom:0px;
  -webkit-transition: all 0.2s ease;
          transition: all 0.2s ease;
}
.toolbarIcon:hover img{
  width:35px;
  height:35px;
  padding-bottom:5px;
}

How about this:

$(".toolbarIcon").mouseover(function(){
    $(this).find("img").css("width","30px").css("height","30px");
});

You can use mouseeneter and mouseleave. see thread

$(".toolbarIcon").mouseeneter(function(){
    $(this).css("background-color","#FFCC80");
    $(this).find("img").height("30").width("30");
});
$(".toolbarIcon").mouseleave(function(){
    $(this).css("background-color","#EBEBFF");
    $(this).find("img").height("40").width("40");
});

I think the following should work

$(document).ready(function(){

 $('.toolbarIcon img').css('width', '60px')
});

or you could give the img an id such img1

$(document).ready(function(){

 $('#img1').css('width', '60px')
});

I ahvent tested this but i think it should work

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top