At this heading I will explain hoe to create such a Sliding Picture Gallery. You need to know some basics of HTML and JavaScript to do this.
Here the link to see the result.First we start to set the positioning of the necessary divisions on the browser screen. We need to use some CSS to do that. Here The codes. We need basically one container division to include all the others then two "movePart" and "fillingPart" divisions. "movePart" will include the picture and it will be slided. "filingPart" will include two more divisions to be used for arrows to change the images. The divisions for arrows are "leftArrow" and "rightArrow".
#container{
position:relative;
width:350px;
height:345px;
border:thin solid;
}
#fillingPart{
position:absolute;
width:350px;
height:25px;
left:0px;
top:0px;
}
#rightArrow{
position:absolute;
width:24px;
height:25px;
background-color:#0C9;
right:0px;
top:0px;
cursor:pointer;
background:url(rightArrow.jpg);
}
#leftArrow{
position:absolute;
width:24px;
height:25px;
background-color:#0C9;
left:0px;
top:0px;
cursor:pointer;
background:url(leftArrow.jpg);
}
#movePart{
position:absolute;
top:25px;
left:0px;
width:350px;
height:320px;
background:#A6BBCD;
overflow:hidden;
}
#fullScreenButton{
cursor:pointer;
}
Now it is time to add HTML components to our design.
<body onload="setImageArray();">
<div id = "container">
<div id = "fillingPart" align="center">
<div id = "rightArrow" onclick="useSlideForward()">
</div>
<table id = "panel" border="0">
<tr>
<td id = "fullScreenButton" align="center" onclick="fullScreen();" onmouseover="mouseOn();" onmouseout="mouseOff()"><font id="sized">Real Sized View</font></td>
</tr>
</table>
<div id = "leftArrow" onclick="useSlideBackward()">
</div>
</div>
<div id="movePart">
</div>
</div>
</body>
At html part we start with calling the function "setImageArray" which we will see late but basically it sets an array of the images that will be used for gallery. It behaves like a constructor. Then as I defined, we create the structure of the divisions. Also we add event for a click over to arrows' divisions to call the functions that change the images on the screen. I used table for setting the certain center alignment to the "fullScreenButton" between the arrows in the "fillingPart". In addition, it calls "fullScreen" function with mouse click to show the image with real size on a new opened window. We have also "movePart" division that will be filled by JavaScript code at the script field of the code.
Now it is time for the script code.
var sliderIntervalId = 0;
var sliderIntervalId2 = 0;
var sliderHeight = 320;
var sliding = false;
var slideSpeed = 10;
var countImage = 0;
var numOfImages = 8;
var forwardOrBack;
function useSlideForward(){
forwardOrBack = true;
slide();
}
function useSlideBackward(){
forwardOrBack = false;
slide();
}
function slide(){
if(sliding)
return;
sliding = true;
if(sliderHeight == 320)
sliderIntervalId = setInterval('slideUpRun()',30);
else
sliderIntervalId = setInterval('slideDownRun()',30);
}
function slideUpRun()
{
slider = document.getElementById('movePart');
if(sliderHeight <= 0)
{
sliding = false;
sliderHeight = 0;
slider.style.height = '0px';
clearInterval(sliderIntervalId);
if(forwardOrBack)
moveForward();
else
moveBackward();
slide();
}
else
{
sliderHeight -= slideSpeed;
if(sliderHeight < 0)
sliderHeight = 0;
slider.style.height = sliderHeight + 'px';
}
}
function slideDownRun()
{
slider = document.getElementById('movePart');
if(sliderHeight >= 320)
{
sliding = false;
sliderHeight = 320;
slider.style.height = '320px';
clearInterval(sliderIntervalId);
//change the image
}
else
{
sliderHeight += slideSpeed;
if(sliderHeight > 320 )
sliderHeight = 320;
slider.style.height = sliderHeight + 'px';
}
}
//***********************
//create the images array
//***********************
var images = [];
function setImageArray(){
var j =0;
for(var i = 0; i < numOfImages; i++){
images[i] = new Image();
images[i].src = "image"+(i+1)+".jpg";
images[i].width = "350";
images[i].height = "320";
}
document.getElementById("movePart").appendChild(images[0]);
}
function moveForward(){
document.getElementById("movePart").removeChild(images[countImage]);
countImage++;
if(countImage >= numOfImages)
countImage = 0;
document.getElementById('movePart').appendChild(images[countImage]);
}
function moveBackward(){
document.getElementById("movePart").removeChild(images[countImage]);
countImage--;
if(countImage < 0)
countImage = 7;
document.getElementById('movePart').appendChild(images[countImage]);
}
var windowm;
function fullScreen(){
windowm = window.open("","Real Sized Image",width = 300, height= 200);
windowm.document.write("
");
windowm.document.write("
Close");
}
//********************
//set the color of the text
//********************
function mouseOn(){
document.getElementById("sized").style.color = "#999999";
}
function mouseOff(){
document.getElementById("sized").style.color = "#000000";
}
We start by defining some of variables of the script.
slideIntervalId -- to keep the id of the setInterval function of the script.
If you do not know setInterval method here the little tutorial.sliderHeight -- it defines the height of the "movePart" division and it will be used to limit its motion.
sliding -- it is used to check whether "movePart" is sliding or not.
slideSpeed -- it is used to define the speed of the motion by using it inside the setInterval() function.
countImage -- it defines index of the imagesArray.
numOfImages -- it is number of the images.
forwardOrBack -- it defines which of the arrow is clicked.If it is true "rightArrow" is clicked otherwise "letfArrow"
Now type functions.
First two functions are called by the clicks over the corresponding arrow. Then they define clicked arrow and call the slide function that slides the galley.
"slide" function checks gallery is on the motion or not firstly. If it is not on motion choose the correct sliding function respected to the current situation of the motion.
Then we come to "slideUpRun" that slides the image to up and "slideDownRun" gives the opposite motion. They are called sequentially by "slide" function. Both of them have same basic algorithm in the function declaration. First we create a "slider" object of the "movePart" division. Then we check the sliderHeight to stop the motion ( as example, for slide up funtion if sliderHeight is 0 then stop the motion.).To stop the motion we set the height of the "movePart" division height 320 px. It makes guarantied to set the height to correct number.Moreover we calls the "clearInterval" function to stop the execution of the "setInterval" function correspond to sliderIntervalId.
In slide up and slide down function we gives the motion with the "else" section of the codes. We start this section with the code segment that increase or the decrease the height of the division for each call. Then we check whether our motion id out of the out limit or not. Finally we set the height of the division to our sliderHeight.
We solved the motion problem of our gallery. Now we get into the problem of setting the images and changing the images. First function of this section is "setImageArray"
that sets the image array and put the initial image to the "movePart" division. Then we have changing image functions with basic algorithms. Next function is "fullScreen" that opens new window with current image's real size. Our final functions are "mouseOn" and "mouseOff" functions that chage the color of the "Real Sized View" text on the top of the script.
That's all. For further help you can contact me. Take care !