我正在为一个不寻常的网站项目制作原型,该项目有以下要求:
出于演示目的,我已将内容区域包装器的高度设置为50%,以便能够更好地说明我在JSFiddle中提供的空间中遇到的问题 - http://jsfiddle.net/f5hgL/
我正在使用jQuery UI滑块组件进行水平滚动,并使用一些额外的自定义JS来计算和设置.scroll-content容器的宽度(需要设置)。如果每个内容框的宽度设置为像素宽度,则可以正常工作。然而,当它是灵活性的百分比时,它会破坏(见演示)。
除了这个问题,我不确定这种布局是可以实现的,甚至是不可取的。无论宽度为100%,100%高度都会产生许多问题,包括滚动条位置和主导航的一致性。
任何人都可以使用jQuery建议另一种方法吗?该网站最初设计为Flash网站,但客户改为HTML / CSS / JS解决方案后,我们建议它更好,原因有很多,包括更简单的CMS集成,SEO和链接到各个页面的能力。 / p>
$(function() {
//scrollpane parts
var scrollPane = $( ".scroll-pane" ),
scrollContent = $( ".scroll-content" );
//set length of conveyor
$(".scroll-content-item").css("width","10%");
var itemwidth = $(".scroll-content-item").width();
var itemcount = $(".scroll-content-item").length;
var itemswidth = itemwidth * itemcount;
var itemsmargins = itemcount * 20; //margin of 0 10px
var itemborders = itemcount * 2; //1px border
var containerwidth = itemsmargins + itemswidth + itemborders + 200 + "px";
//alert("itemsmargins + itemswidth + itemborders = "+containerwidth);
scrollContent.css("width",containerwidth);
//build slider
var scrollbar = $( ".scroll-bar" ).slider({
max: 100,
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
});
答案 0 :(得分:0)