Posted By

mlange on 10/03/06


Tagged

slider


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

shachi
vali29


slider


 / Published in: JavaScript
 

URL: http://www.howtocreate.co.uk/jslibs/slider.js

  1. /********************************************************************
  2.   Slider control creator
  3.   By Mark Wilton-Jones 12-13/10/2002
  4. Version 1.2 updated 13/04/2005 to protect against multi-button clicks
  5. *********************************************************************
  6.  
  7. Please see http://www.howtocreate.co.uk/jslibs/ for details and a demo of this script
  8. Please see http://www.howtocreate.co.uk/jslibs/termsOfUse.html for terms of use
  9.  
  10. To use:
  11.  
  12. Inbetween the <head> tags, put:
  13.  
  14. <script src="PATH TO SCRIPT/slider.js" type="text/javascript" language="javascript1.2"></script>
  15.  
  16. To create a slider put:
  17.  
  18. var myslider = new slider(
  19. 24, //height of track (excluding border)
  20. 150, //width of track (excluding border)
  21. '#9999bb', //colour of track
  22. 2, //thickness of track border
  23. '#770000', //colour of track border
  24. 2, //thickness of runner (in the middle of the track)
  25. '#000000', //colour of runner
  26. 16, //height of button (excluding border)
  27. 16, //width of button (excluding border)
  28. '#dfdfdf', //colour of button
  29. 2, //thickness of button border (shaded to give 3D effect)
  30. '<font size="2">&lt;&gt;</font>', //text of button (if any) - the font declaration is important - size it to suit your slider
  31. true, //direction of travel (true = horizontal [0 is left], false = vertical [0 is top])
  32. 'moveFunction', //the name of the function to execute as the slider moves (null if none)
  33. 'stopFunction', //the name of the function to execute when the slider stops (null if none)
  34. //the functions must have already been defined (or use null for none)
  35. false //optional, false = use default cursor over button, true = use hand cursor
  36. );
  37.  
  38. You can then set the position of the slider after the page has loaded using:
  39.  
  40. myslider.setPosition(numberBetween0and1)
  41. ___________________________________________________________________________________________*/
  42.  
  43. var MWJ_slider_controls = 0;
  44.  
  45. function getRefToDivNest( divID, oDoc ) {
  46. //get a reference to the slider control, even through nested layers
  47. if( !oDoc ) { oDoc = document; }
  48. if( document.layers ) {
  49. if( oDoc.layers[divID] ) { return oDoc.layers[divID]; } else {
  50. for( var x = 0, y; !y && x < oDoc.layers.length; x++ ) {
  51. y = getRefToDivNest(divID,oDoc.layers[x].document); }
  52. return y; } }
  53. if( document.getElementById ) { return document.getElementById(divID); }
  54. if( document.all ) { return document.all[divID]; }
  55. return document[divID];
  56. }
  57.  
  58. function sliderMousePos(e) {
  59. //get the position of the mouse
  60. if( !e ) { e = window.event; } if( !e || ( typeof( e.pageX ) != 'number' && typeof( e.clientX ) != 'number' ) ) { return [0,0]; }
  61. if( typeof( e.pageX ) == 'number' ) { var xcoord = e.pageX; var ycoord = e.pageY; } else {
  62. var xcoord = e.clientX; var ycoord = e.clientY;
  63. if( !( ( window.navigator.userAgent.indexOf( 'Opera' ) + 1 ) || ( window.ScriptEngine && ScriptEngine().indexOf( 'InScript' ) + 1 ) || window.navigator.vendor == 'KDE' ) ) {
  64. if( document.documentElement && ( document.documentElement.scrollTop || document.documentElement.scrollLeft ) ) {
  65. xcoord += document.documentElement.scrollLeft; ycoord += document.documentElement.scrollTop;
  66. } else if( document.body && ( document.body.scrollTop || document.body.scrollLeft ) ) {
  67. xcoord += document.body.scrollLeft; ycoord += document.body.scrollTop; } } }
  68. return [xcoord,ycoord];
  69. }
  70.  
  71. function slideIsDown(e) {
  72. if( document.onmousemove == slideIsMove ) { return false; } //protect against multi-button click
  73. //make note of starting positions and detect mouse movements
  74. window.msStartCoord = sliderMousePos(e); window.lyStartCoord = this.style?[parseInt(this.style.left),parseInt(this.style.top)]:[parseInt(this.left),parseInt(this.top)];
  75. if( document.captureEvents && Event.MOUSEMOVE ) { document.captureEvents(Event.MOUSEMOVE); document.captureEvents(Event.MOUSEUP); }
  76. window.storeMOUSEMOVE = document.onmousemove; window.storeMOUSEUP = document.onmouseup; window.storeLayer = this;
  77. document.onmousemove = slideIsMove; document.onmouseup = slideIsMove; return false;
  78. }
  79.  
  80. function slideIsMove(e) {
  81. //move the slider to its newest position
  82. var msMvCo = sliderMousePos(e); if( !e ) { e = window.event ? window.event : ( new Object() ); }
  83. var theLayer = window.storeLayer.style ? window.storeLayer.style : window.storeLayer; var oPix = document.childNodes ? 'px' : 0;
  84. if( window.storeLayer.hor ) {
  85. var theNewPos = window.lyStartCoord[0] + ( msMvCo[0] - window.msStartCoord[0] );
  86. if( theNewPos < 0 ) { theNewPos = 0; } if( theNewPos > window.storeLayer.maxLength ) { theNewPos = window.storeLayer.maxLength; }
  87. theLayer.left = theNewPos + oPix;
  88. } else {
  89. var theNewPos = window.lyStartCoord[1] + ( msMvCo[1] - window.msStartCoord[1] );
  90. if( theNewPos < 0 ) { theNewPos = 0; } if( theNewPos > window.storeLayer.maxLength ) { theNewPos = window.storeLayer.maxLength; }
  91. theLayer.top = theNewPos + oPix;
  92. }
  93. //run the user's functions and reset the mouse monitoring as before
  94. if( e.type && e.type.toLowerCase() == 'mousemove' ) {
  95. if( window.storeLayer.moveFunc ) { window.storeLayer.moveFunc(theNewPos/window.storeLayer.maxLength); }
  96. } else {
  97. document.onmousemove = storeMOUSEMOVE; document.onmouseup = window.storeMOUSEUP;
  98. if( window.storeLayer.stopFunc ) { window.storeLayer.stopFunc(theNewPos/window.storeLayer.maxLength); }
  99. }
  100. }
  101.  
  102. function setSliderPosition(oPortion) {
  103. //set the slider's position
  104. if( isNaN( oPortion ) || oPortion < 0 ) { oPortion = 0; } if( oPortion > 1 ) { oPortion = 1; }
  105. var theDiv = getRefToDivNest(this.id); if( theDiv.style ) { theDiv = theDiv.style; }
  106. oPortion = Math.round( oPortion * this.maxLength ); var oPix = document.childNodes ? 'px' : 0;
  107. if( this.align ) { theDiv.left = oPortion + oPix; } else { theDiv.top = oPortion + oPix; }
  108. }
  109.  
  110. function slider(oThght,oTwdth,oTcol,oTBthk,oTBcol,oTRthk,oTRcol,oBhght,oBwdth,oBcol,oBthk,oBtxt,oAlgn,oMf,oSf,oCrs) {
  111. //draw the slider using huge amounts of nested layers (makes the borders look normal in as many browsers as possible)
  112. if( document.layers ) {
  113. document.write(
  114. '<ilayer left="0" top="0" height="'+(oThght+(2*oTBthk))+'" width="'+(oTwdth+(2*oTBthk))+'" bgcolor="'+oTBcol+'">'+
  115. '<ilayer left="'+oTBthk+'" top="'+oTBthk+'" height="'+oThght+'" width="'+oTwdth+'" bgcolor="'+oTcol+'">'+
  116. '<layer left="'+(oAlgn?0:Math.floor((oTwdth-oTRthk)/2))+'" top="'+(oAlgn?Math.floor((oThght-oTRthk)/2):0)+'" height="'+(oAlgn?oTRthk:oThght)+'" width="'+(oAlgn?oTwdth:oTRthk)+'" bgcolor="'+oTRcol+'"><\/layer>'+
  117. '<layer left="'+(oAlgn?0:Math.floor((oTwdth-(oBwdth+(2*oBthk)))/2))+'" top="'+(oAlgn?Math.floor((oThght-(oBhght+(2*oBthk)))/2):0)+'" height="'+(oBhght+(2*oBthk))+'" width="'+(oBwdth+(2*oBthk))+'" bgcolor="#000000" onmouseover="this.captureEvents(Event.MOUSEDOWN);this.hor='+oAlgn+';this.maxLength='+((oAlgn?oTwdth:oThght)-((oAlgn?oBwdth:oBhght)+(2*oBthk)))+';this.moveFunc='+oMf+';this.stopFunc='+oSf+';this.onmousedown=slideIsDown;" name="MWJ_slider_controls'+MWJ_slider_controls+'">'+
  118. '<layer left="0" top="0" height="'+(oBhght+oBthk)+'" width="'+(oBwdth+oBthk)+'" bgcolor="#ffffff"><\/layer>'+
  119. '<layer left="'+oBthk+'" top="'+oBthk+'" height="'+oBhght+'" width="'+oBwdth+'" bgcolor="'+oBcol+'">'+
  120. oBtxt+'<\/layer><\/layer><\/ilayer><\/ilayer>'
  121. );
  122. } else {
  123. document.write(
  124. '<div style="position:relative;left:0px;top:0px;height:'+(oThght+(2*oTBthk))+'px;width:'+(oTwdth+(2*oTBthk))+'px;background-color:'+oTBcol+';font-size:0px;">'+
  125. '<div style="position:relative;left:'+oTBthk+'px;top:'+oTBthk+'px;height:'+oThght+'px;width:'+oTwdth+'px;background-color:'+oTcol+';font-size:0px;">'+
  126. '<div style="position:absolute;left:'+(oAlgn?0:Math.floor((oTwdth-oTRthk)/2))+'px;top:'+(oAlgn?Math.floor((oThght-oTRthk)/2):0)+'px;height:'+(oAlgn?oTRthk:oThght)+'px;width:'+(oAlgn?oTwdth:oTRthk)+'px;background-color:'+oTRcol+';font-size:0px;"><\/div>'+
  127. '<div style="position:absolute;left:'+(oAlgn?0:Math.floor((oTwdth-(oBwdth+(2*oBthk)))/2))+'px;top:'+(oAlgn?Math.floor((oThght-(oBhght+(2*oBthk)))/2):0)+'px;height:'+(oBhght+(2*oBthk))+'px;width:'+(oBwdth+(2*oBthk))+'px;font-size:0px;" ondragstart="return false;" onselectstart="return false;" onmouseover="this.hor='+oAlgn+';this.maxLength='+((oAlgn?oTwdth:oThght)-((oAlgn?oBwdth:oBhght)+(2*oBthk)))+';this.moveFunc='+oMf+';this.stopFunc='+oSf+';this.onmousedown=slideIsDown;" id="MWJ_slider_controls'+MWJ_slider_controls+'">'+
  128. '<div style="border-top:'+oBthk+'px solid #ffffff;border-left:'+oBthk+'px solid #ffffff;border-right:'+oBthk+'px solid #000000;border-bottom:'+oBthk+'px solid #000000;">'+
  129. '<div style="height:'+oBhght+'px;width:'+oBwdth+'px;font-size:0px;background-color:'+oBcol+';cursor:'+(oCrs?'pointer;cursor:hand':'default')+';">'+
  130. '<span style="width:100%;text-align:center;">'+oBtxt+'<\/span><\/div><\/div><\/div><\/div><\/div>'
  131. );
  132. }
  133. this.id = 'MWJ_slider_controls'+MWJ_slider_controls; this.maxLength = (oAlgn?oTwdth:oThght)-((oAlgn?oBwdth:oBhght)+(2*oBthk));
  134. this.align = oAlgn; this.setPosition = setSliderPosition; MWJ_slider_controls++;
  135. }

Report this snippet  

You need to login to post a comment.