Wednesday, July 14, 2010

Simple Image Slider
















Recently, I have created image slider for a website like one displayed above. User can navigate to particular image by clicking on an image number otherwise Images to rotate one after other. When a user hovers a mouse over a number, image rotation stops. On mouse out rotation resumed.


Here are the code and description of my approach to implement this type of slider.

First of all, the image name should be same except last few digits like as displayed below.

e.g shoes_image1.jpg, shoes_image2.jpg, shoes_image1.jpg

So that you can synchronize image rotation through a loop using some variable. To begin slider a JavaScript function called from body onLoad method.


<body onload="slideImage();" onunload="clearTimer();">

Here slideImage function initiates sliding.

Here is the complete JavaScript code.

var _currentImage=0;
var _currentBrand
var _interval;
function changeImage(_imgNumber)
{
for(var i=1;i<=5;i++)

{
document.getElementById('imgCell'+i).style.border='1px solid black';
}
document.getElementById('images').style.background='url(plastic_parts_'+_imgNumber+'.jpg) no-repeat';
document.getElementById('imgCell'+_imgNumber).style.border='';
}

function slideImage()
{
_currentImage++;
if(_currentImage>4)
{
_currentImage=1;
}
document.getElementById('images').style.background='url(plastic_parts_'+_currentImage+'.jpg) no-repeat';
_interval=setTimeout("slideImage()",3000);
}

function clearTimer()
{
clearTimeout(_interval);
}

function resetTimer()
{
_interval=setTimeout("slideImage()",3000);
}

and here is HTML code.

<div id="images" style="border-width: 1px; border-style: solid; border-color: black; width:500px; height:500px; overflow:hidden; background: url(plastic_parts_1.jpg) no-repeat;">
<table style="position:relative; top:460px; left:10px; background-color:#EAEAEA;">
<tr>
<td id="imgCell1" style="border: 1px solid black; width:20px;"><center><a style="color:black;" href="javascript:" onclick="changeImage(1);" onMouseOver="clearTimer();" onMouseOut="resetTimer();">1</a></center></td>
<td id="imgCell2" style="border: 1px solid black; width:20px;"><center><a style="color:black;" href="javascript:" onclick="changeImage(2);" onMouseOver="clearTimer();" onMouseOut="resetTimer();">2</a></center></td>
<td id="imgCell3" style="border: 1px solid black; width:20px;"><center><a style="color:black;" href="javascript:" onclick="changeImage(3);" onMouseOver="clearTimer();" onMouseOut="resetTimer();">3</a></center></td>
<td id="imgCell4" style="border: 1px solid black; width:20px;"><center><a style="color:black;" href="javascript:" onclick="changeImage(4);" onMouseOver="clearTimer();" onMouseOut="resetTimer();">4</a></center></td>
</tr>
</table>
</div>


Hope this helps. Let me know if you have any questions.


Friday, June 4, 2010

Change CSS class from JavaScript

Recently, I was working on Magento Customization. I encountered a small problem. The css class which was applied to div tag by Magento was disturbing whole layout on particular page. So I need to set my own class over there.

I was having two options.

1) Change css class in Magento core file. However, this was impossible as this change will affect all other pages which was not desirable.

2) Change css class on particular page when page loads.

I choose second option. OnLoad of a body I called a JavaScript function. In JavaScript, I have used following code to change class.

document.getElementById('myDiv').className='newClassName';

When page loads first time, it will be a disturbed layout but once body on load event gets fired the layout will be proper.

Thursday, May 27, 2010

My Experience with document.getElementById()

Hello friends,

I would like to share one interesting experience with you.

Currently, I was working with Asp.Net tree view. The data in tree view is very large, so we implemented search functionality in tree view. User can add text of a node to be searched. If more than one node found, each node will be checked in tree view. As I have mentioned the data in a tree is very large, so we have kept this tree inside a panel so that user can scroll up or down to view entire tree view.

Then one more client requirement came. When a node found during a search, panel should be scrolled down to location of that particular node in tree view. For that we have used document.getElementById('TreeView1_SelectedNode') to get selected node. Then using its location. we have scrolled panel.

Here comes interesting part. What if I have more than one selected node. I was under the impression that it should return first selected node and ignore the rest. However, I was surprised that it returns me last selected node and panel scrolled down to last selected node.