Write javascript code to make an image increase to 120% on mouseover
1. function onMouseOver(){
2. var img = document
3. img
4. var w = img
5. var h = img
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
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.
10. var xOffset = w
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
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
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

No comments.