2011-01-10 180 views
0

我想实现JQ UIslider - 但我一直遇到问题,所以我希望有人可以帮助。JQuery滑块问题

我试图做到没有“滑块”按钮的JQUI图像。我可以得到这么多,但我无法让它“正常”工作。我的代码是这样的:

HTML:

<div id="scroll-pane" class="clearfix" > 
<div id="pagination" class="clearfix"></div> 
<div class="scroll-bar-wrap"> 
<div class="scroll-bar"></div></div> 
</div> 

CSS:

.scroll-bar { background-color:#00FFFF; width: 50px; height: 20px; } 
#pagination { width: 2440px; float: left; } 
#scroll-pane { overflow: auto; width: 99%; float:left; } 
.scroll-bar-wrap { clear: left; padding: 0 4px 0 2px; margin: 0 -1px -1px -1px; background: #FFCC00; width: 400px; } 
.scroll-bar-wrap .ui-slider { border:0; height: 2em; margin: 0 auto; background: #FF0000; width: 20px;} 

和JQ是这样的:(顺便说一句我一句都没听懂了吧!)

$(document).ready(function() { 

    var scrollPane = $("#scroll-pane"), 
    scrollContent = $("#pagination"); 
    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 
}); 

#pagination的内容是“动态”生成的。我的问题是,.scroll-bar-wrap .ui-slider不滑动(动画)。只是“触摸它”会过度快速地移动幻灯片内容,很难“恢复”。

我需要的是一个“简单滑块情境”,其中#页面向左/向右移动以在创建页面时显示/隐藏页码。有些东西告诉我JQUI代码是“过于复杂” - 我可能是错的 - 但到目前为止,这已经花费了很多时间在很简单的事情上 - 好吧,我不是JQ/JS类型的人,但是。 。JQUI示例link text正常工作,这是它的代码。我只是不希望背景/滑块按钮,因为他们有它。

回答

0

如果这是太复杂了,你可以使用slideTo插件:http://plugins.jquery.com/project/slideto

环绕在你的周围分页格的页编号的跨度或股利,以确保scrollto可以滚动到它。

<div id="pagination"> 
    <span class="pagenumber">1</span> 
    <span class="pagenumber">2</span> 
    <span class="pagenumber">3</span> 
</div> 

然后,您可以建立一个扩展了一些滚动的能力在div#分页一个jQuery对象,这样就可以记住滚动状态,做这样的事情:

pageNumbers = $("#pagination").find("span.pagenumber"); 
... 
$("#pagination").scrollTo(pageNumbers.next()); 

你必须得到一些jQuery的知识,让这个工作正常!如果你可以更彻底地解释你想要达到的目标,我可以帮助你。