JQuery slider problems
I am trying to implement JQ UIslider - but I keep running into problems, so I hope that someone can help.
I am trying to do it without the JQUI image for the "slider" button. I can get so far but I cannot get it to work "properly". My code is like this:
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;}
and the JQ is like this: (BTW I don't understand any of it!)
$(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( scrollCo开发者_开发问答ntent.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
});
The content for #pagination is "dynamically" generated. The problem I have is that .scroll-bar-wrap .ui-slider
doesn't slide (animate). Just "touching it" moves the slide content too much and too quickly and it is very difficult to "get it back".
All I need is a "simple slider situation" where #pagination moves left/right to show/hide page numbers as they are created. Something tells me the JQUI code is "over complex" - I might be wrong - but so far this has taken up far far to much time on what should be a simple thing - OK granted I am not a JQ/JS type person but .. the JQUI example link text works fine and this is the code for it. I just don't want the backgrounds/slider button as they have it.
If this is too complex, you could use the slideTo plugin: http://plugins.jquery.com/project/slideto
Wrap a span or div around every page number in your pagination div, to make sure scrollto can scroll to it.
<div id="pagination">
<span class="pagenumber">1</span>
<span class="pagenumber">2</span>
<span class="pagenumber">3</span>
</div>
You can then build a jQuery object that extends the div#pagination with some scrolling abilities, so that you can remember the scrolling state and do something like this:
pageNumbers = $("#pagination").find("span.pagenumber");
...
$("#pagination").scrollTo(pageNumbers.next());
You'll have to get some jQuery knowledge to get this to work properly though! If you can explain more thoroughly what you're trying to achieve, I can help you on your way.
精彩评论