Image shaking effect with javascript. Today’s lesson quite entertaining, we’ll learn how to construct a shaking effect. For clarity, we will apply this effect to the picture. You will need to hold down by mouse a certain area in the image and move it to another location (drag). Now, I sure that better to see our online demonstration. How did it achieve? In principle, simply enough, the entire ‘image’ divided into 4 sectors. Place, where we will begin to drag by mouse – is the boundary separating of our four pictures. And shaking effect itself – will only change the sizes of our sectors and pictures in them. Read more.
Here are samples and downloadable package:
[sociallocker]
[/sociallocker]
Ok, download the example files and lets start coding !
Step 1. HTML
As usual, we start with the HTML. This is source code of our sample:
index.html
03 | <title>Image shaking effect using pure JavaScript</title> |
04 | <link rel="stylesheet" type="text/css" href="css/main.css" /> |
05 | <script type="text/javascript" src="js/main.js"></script> |
09 | <div id="main_object"> |
10 | <div class="tl"><img alt="" src="image.jpg"></div> |
11 | <div class="tr"><img alt="" src="image.jpg"></div> |
12 | <div class="bl"><img alt="" src="image.jpg"></div> |
13 | <div class="br"><img alt="" src="image.jpg"></div> |
Here are main object with 4 images inside
Step 2. CSS
Here are single CSS file with all necessary styles:
css/main.css
1 | body{background:#eee;margin:0;padding:0} |
2 | .example{background:#FFF;width:900px;border:1px #000 solid;margin:20px auto;padding:15px;-moz-border-radius: 3px;-webkit-border-radius: 3px} |
3 | #main_object{position:relative;width:900px;height:675px;overflow:hidden;cursor:pointer} |
4 | #main_object div{position:absolute;overflow:hidden} |
5 | #main_object img{position:absolute;-ms-interpolation-mode:nearest-neighbor} |
6 | #main_object .tl,#main_object .tl img{top:0;left:0} |
7 | #main_object .tr,#main_object .tr img{top:0;right:0} |
8 | #main_object .bl,#main_object .bl img{bottom:0;left:0} |
9 | #main_object .br,#main_object .br img{right:0;bottom:0} |
Step 3. JS
Here are our main Javascript:
js/main.js
019 | this.mobj = document.getElementById('main_object'); |
020 | var div = this.mobj.getElementsByTagName('div'); |
023 | for (var i = 0; i<4; i++) { |
024 | this.d[i] = div[i].style; |
025 | this.i[i] = div[i].getElementsByTagName('img')[0].style; |
030 | document.onselectstart = function() { |
034 | this.mobj.ondrag = function() { |
038 | this.mobj.onmousedown = function() { |
040 | ishk.mobj.style.cursor = 'move'; |
041 | ishk.xd = ishk.xm - ishk.nx; |
042 | ishk.yd = ishk.ym - ishk.ny; |
046 | document.onmouseup = function() { |
048 | ishk.mobj.style.cursor = 'pointer'; |
052 | this.i[3].width = ishk.nw; |
053 | this.i[3].height = ishk.nh; |
061 | mousemove : function(e) { |
066 | resize : function() { |
068 | for (ishk.nx = 0, ishk.ny = 0; o != null; o = o.offsetParent) |
069 | ishk.nx += o.offsetLeft, ishk.ny += o.offsetTop; |
070 | ishk.nw = ishk.mobj.offsetWidth; |
071 | ishk.nh = ishk.mobj.offsetHeight; |
080 | ishk.svx = ishk.speedD * ishk.svx - (ishk.sx - ishk.xd - ishk.nx) / ishk.speedX; |
081 | ishk.svy = ishk.speedD * ishk.svy - (ishk.sy - ishk.yd - ishk.ny) / ishk.speedY; |
086 | var w0 = Math.max(0, Math.round(ishk.sx) - ishk.nx); |
087 | var h0 = Math.max(0, Math.round(ishk.sy) - ishk.ny); |
088 | var w1 = Math.max(0, ishk.nw - (Math.round(ishk.sx) - ishk.nx)); |
089 | var h1 = Math.max(0, ishk.nh - (Math.round(ishk.sy) - ishk.ny)); |
090 | var w2 = Math.max(0, Math.round((w0 * ishk.nw) / ishk.xd)); |
091 | var h2 = Math.max(0, Math.round((h0 * ishk.nh) / ishk.yd)); |
092 | var w3 = Math.max(0, Math.round((w1 * ishk.nw) / (ishk.nw - ishk.xd))); |
093 | var h3 = Math.max(0, Math.round((h1 * ishk.nh) / (ishk.nh - ishk.yd))); |
095 | ishk.d[0].width = w0 + 'px'; |
096 | ishk.d[1].width = w1 + 'px'; |
097 | ishk.d[2].width = w0 + 'px'; |
098 | ishk.d[3].width = w1 + 'px'; |
099 | ishk.d[0].height = h0 + 'px'; |
100 | ishk.d[1].height = h0 + 'px'; |
101 | ishk.d[2].height = h1 + 'px'; |
102 | ishk.d[3].height = h1 + 'px'; |
103 | ishk.i[0].width = w2 + 'px'; |
104 | ishk.i[1].width = w3 + 'px'; |
105 | ishk.i[2].width = w2 + 'px'; |
106 | ishk.i[3].width = w3 + 'px'; |
107 | ishk.i[0].height = h2 + 'px'; |
108 | ishk.i[1].height = h2 + 'px'; |
109 | ishk.i[2].height = h3 + 'px'; |
110 | ishk.i[3].height = h3 + 'px'; |
112 | setTimeout(ishk.shake, 20); |
115 | window.onload = function() { |
118 | document.onmousemove = function(e) { |
119 | if (window.event) e = window.event; |
123 | window.onresize = ishk.resize; |
It is rather simple. When the page loads – I initialize our main object, and link all the necessary events. Then, after initialization, I loop our main ‘shake’ function, which changing sizes of all our 4 images (sectors).
Step 4. Images
For our demo I used only one image:

Conclusion
Today I told you how to create easy shaking effect to images. Commonly – you can try to play with any another objects too. Hope our javascript lessons still interesting for you. Good luck!