NotePD Loader
Ideas Post

Write javascript code to make an image increase to 120% on mouseover

    1. function onMouseOver(){

    function onMouseOver(){

    2. var img = document

    getElementById("image");

    3. img

    onmouseover = function(){

    4. var w = img

    width;

    5. var h = img

    height;

    6. var newW = 120; //new width to make image bigger than original width

    7. var newH = 120; //new height to make image bigger than original height

    8. var scaleFactor = (w / newW) * 100 + " %"; //scale factor is the percentage of the increase in size

    This is used to calculate how much smaller to shrink the image so it increases in size when you mouse over it
    See next step for that formula

    9. var scaleFactor2 = (h / newH) * 100 + " %"; //same thing as above but for height instead of width. This is used in the next step below.

    1

    10. var xOffset = w

    (newW
    img
    width); //if newW > w then we have to move the image left a bit so it doesn't overlap with where it was before we increased its size
    1

    11. var yOffset = h

    (newH
    img
    height); //same thing for top and bottom edge of image vs where it was before we increased its size 1

    12. if(xOffset > 0 && yOffset > 0){ //we moved it, so now we need to shrink it back down so that when we increase its size, it increases in size instead of staying the same size and just moving from one place to another on screen

    13 : img
    style
    filter = " pixelate(" + scaleFactor2 + ", " + scaleFactor + ") " ; //this makes the image smaller using this formula above, then increasing its size back up again using this next line 14 : img
    style
    filter = "" ; //removes any pixelation applied above by making an empty string as part of filter="" 15 : } 16 : } 17 : 18 : function onMouseOut(){ 19 : document
    getElementById("image")
    onmouseout=null; 20 : } 21
0 Like.0 Comment
Comment
Branch
Repost
Like
Comment
Branch
Like
0
40015
0
0
Comments (0)

No comments.