Here are sample and downloadable package:
Ok, download the example files and lets start coding !
Step 1. HTML
As usual, we start with the HTML.
This is our main page code with all samples.
index.html
01 |
< link rel = "stylesheet" href = "css/main.css" type = "text/css" /> |
02 |
< script src = "js/script.js" ></ script > |
05 |
< h3 >< a href = "#" >Photo inspiration (zoom fading) example</ a > < button id = "mode" style = "float:right" >Change mode</ button ></ h3 > |
07 |
< div id = "panel" ></ div > |
08 |
< div id = "imgsource" style = "display:none;" > |
09 |
< img src = "data_images/pic1.jpg" /> |
10 |
< img src = "data_images/pic2.jpg" /> |
11 |
< img src = "data_images/pic3.jpg" /> |
12 |
< img src = "data_images/pic4.jpg" /> |
13 |
< img src = "data_images/pic5.jpg" /> |
14 |
< img src = "data_images/pic6.jpg" /> |
15 |
< img src = "data_images/pic7.jpg" /> |
16 |
< img src = "data_images/pic8.jpg" /> |
17 |
< img src = "data_images/pic9.jpg" /> |
18 |
< img src = "data_images/pic10.jpg" /> |
We prepared and hide our set of using images. Plus prepared panel area for drawing.
Step 2. CSS
Here are used CSS styles.
css/main.css
01 |
body{ background : #eee ; font-family : Verdana , Helvetica , Arial , sans-serif ; margin : 0 ; padding : 0 } |
02 |
.example{ background : #FFF ; width : 770px ; height : 565px ; font-size : 80% ; border : 1px #000 solid ; margin : 20px auto ; padding : 15px ; position : relative ;-moz-border-radius: 3px ;-webkit-border-radius: 3px } |
12 |
-ms-interpolation-mode:nearest-neighbor; |
13 |
image-rendering: optimizeSpeed; |
Step 3. JS
Here are our main control JS file.
js/main.js
018 |
init : function (iMode) { |
020 |
this .oImgSource = document.getElementById( 'imgsource' ).children; |
021 |
this .oPan = document.getElementById( 'panel' ); |
022 |
this .iPanWidth = this .oPan.offsetWidth; |
023 |
this .iPanHeight = this .oPan.offsetHeight; |
026 |
this .oImgBefore = document.createElement( 'img' ); |
027 |
this .oImgBefore.src = this .oImgSource[0].src; |
028 |
this .oImgBefore.style.top = ( this .bVZoom ? - this .iMaxZoom : 0) + 'px' ; |
029 |
this .oImgBefore.style.left = ( this .bHZoom ? - this .iMaxZoom : 0) + 'px' ; |
030 |
this .oImgBefore.style.width = ( this .bHZoom ? this .iPanWidth + (2 * this .iMaxZoom) : this .iPanWidth) + 'px' ; |
031 |
this .oImgBefore.style.height = ( this .bVZoom ? this .iPanHeight + (2 * this .iMaxZoom) : this .iPanWidth) + 'px' ; |
032 |
this .oImgBefore.style.filter = 'alpha(opacity=1)' ; |
033 |
this .oImgBefore.style.opacity = 1; |
034 |
this .oPan.appendChild( this .oImgBefore); |
037 |
this .oImgAfter = document.createElement( 'img' ); |
038 |
this .oImgAfter.src = this .oImgSource[1].src; |
039 |
this .oImgAfter.style.top = '0px' ; |
040 |
this .oImgAfter.style.left = '0px' ; |
041 |
this .oImgAfter.style.width = this .iPanWidth + 'px' ; |
042 |
this .oImgAfter.style.height = this .iPanHeight + 'px' ; |
043 |
this .oImgAfter.style.filter = 'alpha(opacity=0)' ; |
044 |
this .oImgAfter.style.opacity = 0; |
045 |
this .oPan.appendChild( this .oImgAfter); |
047 |
setInterval(insp.run, this .iTimeInterval); |
051 |
changemode : function (){ |
052 |
this .iMode = ( this .iMode == 2) ? 1 : 2; |
057 |
if (insp.iMaxZoom > insp.iZStep++) { |
059 |
insp.oImgBefore.style.left = (parseInt(insp.oImgBefore.style.left) - 1) + 'px' ; |
060 |
insp.oImgBefore.style.width = (parseInt(insp.oImgBefore.style.width) + 2) + 'px' ; |
061 |
if (insp.iMode == 2) { |
062 |
insp.oImgAfter.style.left = (parseInt(insp.oImgAfter.style.left) - 1) + 'px' ; |
063 |
insp.oImgAfter.style.width = (parseInt(insp.oImgAfter.style.width) + 2) + 'px' ; |
067 |
insp.oImgBefore.style.top = (parseInt(insp.oImgBefore.style.top) - 1) + 'px' ; |
068 |
insp.oImgBefore.style.height = (parseInt(insp.oImgBefore.style.height) + 2) + 'px' ; |
069 |
if (insp.iMode == 2) { |
070 |
insp.oImgAfter.style.top = (parseInt(insp.oImgAfter.style.top) - 1) + 'px' ; |
071 |
insp.oImgAfter.style.height = (parseInt(insp.oImgAfter.style.height) + 2) + 'px' ; |
074 |
if (insp.oImgAfter.filters) |
075 |
insp.oImgAfter.filters.item(0).opacity = Math.round(insp.iZStep / insp.iMaxZoom * 100); |
077 |
insp.oImgAfter.style.opacity = insp.iZStep / insp.iMaxZoom; |
082 |
if (insp.iMode == 2) { |
083 |
insp.oImgAfter.style.left = '0px' ; |
084 |
insp.oImgAfter.style.width = insp.iPanWidth + 'px' ; |
086 |
insp.oImgBefore.style.left = (insp.iMode == 2 ? - insp.iMaxZoom : 0) + 'px' ; |
087 |
insp.oImgBefore.style.width = (insp.iMode == 2 ? (insp.iPanWidth + (2 * insp.iMaxZoom)) : insp.iPanWidth) + 'px' ; |
090 |
if (insp.iMode == 2) { |
091 |
insp.oImgAfter.style.top = '0px' ; |
092 |
insp.oImgAfter.style.height = insp.iPanHeight + 'px' ; |
095 |
insp.oImgBefore.style.top = (insp.iMode == 2 ? - insp.iMaxZoom : 0) + 'px' ; |
096 |
insp.oImgBefore.style.height = (insp.iMode == 2 ? (insp.iPanHeight + (2 * insp.iMaxZoom)) : insp.iPanHeight) + 'px' ; |
098 |
if (insp.oImgAfter.filters) |
099 |
insp.oImgAfter.filters.item(0).opacity = 0; |
101 |
insp.oImgAfter.style.opacity = 0; |
102 |
insp.oImgBefore.src = insp.oImgAfter.src; |
103 |
insp.oImgAfter.src = insp.oImgSource[++insp.iImgStep % insp.oImgSource.length].src; |
109 |
onload = function () { |
112 |
document.getElementById( 'mode' ).onclick = function (){ |
Hope this code is pretty easy. During time we just enlarging images, changing its opacity. All necessary variables collected in top of object. Welcome to experiment!
Conclusion
Hope that you was happy to play with our javascript lessons. I hope that fading switching photos looks fine If is you were wondering – do not forget to thank. Will glad to listen your interesting comments. Good luck!