一个页面上有多个jQuery UI Sliders

时间:2011-12-30 01:36:09

标签: jquery jquery-ui jquery-ui-slider

我真的很难在同一页面上实现多个jQuery UI滑块。每当我移动任何滑块时,所有滑块的内容项都会滑动,而不是仅位于我想要滑动的项目下方的内容项。

示例:

<!--Box 1-->
<div class="scroll-pane">
<div class="scroll-content">
  <ul>
  <li>Item One</li>
  <li>Item Two</li>
  <li>Item Three</li>
  <div class="scroll-bar-wrap ui-widget-content ui-corner-bottom">
  <div class="scroll-bar"></div>
  </div>
</ul>
</div>
</div>

<!--Box 2-->
<div class="scroll-pane">
<div class="scroll-content">
<ul>
  <li>Item One</li>
  <li>Item Two</li>
  <li>Item Three</li>
  <div class="scroll-bar-wrap ui-widget-content ui-corner-bottom">
  <div class="scroll-bar"></div>
  </div>
</ul>
</div>
</div>

这是我的javscript:

<script>
$(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
});
</script>

我真的可以利用某人的帮助来弄清楚如何让其中一个项目滚动而不是两者。

由于

0 个答案:

没有答案