Mossawir Ahmed
Mossawir Ahmed

Reputation: 655

How to add multiple Jquery UI sliders on a page

kindly check this example of jquery UI slider. i am using three sliders on a single page. when i scroll slider other sliders also move. i drag one scroll other two scroll remain on same place but their inner content scroll. there is alot of code in the example. how can i use three sliders with minimal redundant code. kindly see example below and click view source link on the page.

http://jqueryui.com/slider/#side-scroll

sample code

 $(function() {
//scrollpane parts
var scrollPane = $( ".scroll-pane" ),
  scrollContent = $( ".scroll-content" );

//build slider
var scrollbar = $( ".scroll-bar" ).slider({
  slide: function( event, ui ) {
    if ( scrollContent.width() > scrollPane.width() ) {
      scrollContent.css( "margin-left", Math.round(
        ui.value / 100 * ( scrollPane.width() - scrollContent.width() )
      ) + "px" );
    } else {
      scrollContent.css( "margin-left", 0 );
    }
  }
});

//append icon to handle
var handleHelper = scrollbar.find( ".ui-slider-handle" )
.mousedown(function() {
  scrollbar.width( handleHelper.width() );
})
.mouseup(function() {
  scrollbar.width( "100%" );
})
.append( "<span class='ui-icon ui-icon-grip-dotted-vertical'></span>" )
.wrap( "<div class='ui-handle-helper-parent'></div>" ).parent();

//change overflow to hidden now that slider handles the scrolling
scrollPane.css( "overflow", "hidden" );

//size scrollbar and handle proportionally to scroll distance
function sizeScrollbar() {
  var remainder = scrollContent.width() - scrollPane.width();
  var proportion = remainder / scrollContent.width();
  var handleSize = scrollPane.width() - ( proportion * scrollPane.width() );
  scrollbar.find( ".ui-slider-handle" ).css({
    width: handleSize,
    "margin-left": -handleSize / 2
  });
  handleHelper.width( "" ).width( scrollbar.width() - handleSize );
}

//reset slider value based on scroll content position
function resetValue() {
  var remainder = scrollPane.width() - scrollContent.width();
  var leftVal = scrollContent.css( "margin-left" ) === "auto" ? 0 :
    parseInt( scrollContent.css( "margin-left" ) );
  var percentage = Math.round( leftVal / remainder * 100 );
  scrollbar.slider( "value", percentage );
}

//if the slider is 100% and window gets larger, reveal content
function reflowContent() {
    var showing = scrollContent.width() + parseInt( scrollContent.css( "margin-left" ), 10 );
    var gap = scrollPane.width() - showing;
    if ( gap > 0 ) {
      scrollContent.css( "margin-left", parseInt( scrollContent.css( "margin-left" ), 10 ) + gap );
    }
}

//change handle position on window resize
$( window ).resize(function() {
  resetValue();
  sizeScrollbar();
  reflowContent();
});
//init scrollbar size
setTimeout( sizeScrollbar, 10 );//safari wants a timeout
    });

Upvotes: 1

Views: 426

Answers (1)

Mossawir Ahmed
Mossawir Ahmed

Reputation: 655

ok i figured it out!!.. @mark this i was talking about bro. first take every thing in your own function. then in document .ready call it against your unique class like this

$(document).ready(function(){
 sliderMos(".slider1"); 
  sliderMos(".slider2");
  sliderMos(".slider3");
});

here is the rest of the function. call it out the document .ready. thats it

function sliderMos ( sliders ) {

//scrollpane parts
var scrollPane = $(sliders+" .scroll-pane" ),
  scrollContent = $(sliders+" .scroll-content" );
  //scrollPane.hide()
//build slider
var scrollbar = $(sliders+" .scroll-bar" ).slider({
  slide: function( event, ui ) {
    if ( scrollContent.width() > scrollPane.width() ) {
      scrollContent.css( "margin-left", Math.round(
        ui.value / 100 * ( scrollPane.width() - scrollContent.width() )
      ) + "px" );
    } else {
      scrollContent.css( "margin-left", 0 );
    }
  }
});


//append icon to handle
var handleHelper = scrollbar.find( ".ui-slider-handle" )
.mousedown(function() {
  scrollbar.width( handleHelper.width() );
})
.mouseup(function() {
  scrollbar.width( "100%" );
})
.append( "<span class='ui-icon ui-icon-grip-dotted-vertical'></span>" )
.wrap( "<div class='ui-handle-helper-parent'></div>" ).parent();

//change overflow to hidden now that slider handles the scrolling
scrollPane.css( "overflow", "hidden" );

//size scrollbar and handle proportionally to scroll distance
function sizeScrollbar() {
  var remainder = scrollContent.width() - scrollPane.width();
  var proportion = remainder / scrollContent.width();
  var handleSize = scrollPane.width() - ( proportion * scrollPane.width() );
  scrollbar.find( ".ui-slider-handle" ).css({
    width: handleSize,
    "margin-left": -handleSize / 2
  });
  handleHelper.width( "" ).width( scrollbar.width() - handleSize );
}

//reset slider value based on scroll content position
function resetValue() {
  var remainder = scrollPane.width() - scrollContent.width();
  var leftVal = scrollContent.css( "margin-left" ) === "auto" ? 0 :
    parseInt( scrollContent.css( "margin-left" ) );
  var percentage = Math.round( leftVal / remainder * 100 );
  scrollbar.slider( "value", percentage );
}

//if the slider is 100% and window gets larger, reveal content
function reflowContent() {
    var showing = scrollContent.width() + parseInt( scrollContent.css( "margin-left" ), 10 );
    var gap = scrollPane.width() - showing;
    if ( gap > 0 ) {
      scrollContent.css( "margin-left", parseInt( scrollContent.css( "margin-left" ), 10 ) + gap );
    }
}
   $(sliders+" .ui-slider-handle ").append("<span class='dragStyle'>Drag</span>")
//change handle position on window resize
$( window ).resize(function() {
  resetValue();
  sizeScrollbar();
  reflowContent();
});
//init scrollbar size
setTimeout( sizeScrollbar, 10 );//safari wants a timeout


  }

Upvotes: 1

Related Questions