带有轮播的多行
Multiple rows with jcarousel
我正在尝试使用 jcarousel 构建一个包含多行的容器,我已经尝试了一些方法但没有运气。任何人都可以就如何创建它提出任何建议吗?
根据@Sike和我的一点附加.js代码替换,高度不是动态设置的,现在是。
var defaults = {
vertical: false,
rtl: false,
start: 1,
offset: 1,
size: null,
scroll: 3,
visible: null,
animation: 'normal',
easing: 'swing',
auto: 0,
wrap: null,
initCallback: null,
setupCallback: null,
reloadCallback: null,
itemLoadCallback: null,
itemFirstInCallback: null,
itemFirstOutCallback: null,
itemLastInCallback: null,
itemLastOutCallback: null,
itemVisibleInCallback: null,
itemVisibleOutCallback: null,
animationStepCallback: null,
buttonNextHTML: '<div></div>',
buttonPrevHTML: '<div></div>',
buttonNextEvent: 'click',
buttonPrevEvent: 'click',
buttonNextCallback: null,
buttonPrevCallback: null,
moduleWidth: null,
rows: null,
itemFallbackDimension: null
}, windowLoaded = false;
this.clip.addClass(this.className('jcarousel-clip')).css({
position: 'relative',
height: this.options.rows * this.options.moduleWidth
});
this.container.addClass(this.className('jcarousel-container')).css({
position: 'relative',
height: this.options.rows * this.options.moduleWidth
});
if (li.size() > 0) {
var moduleCount = li.size();
var wh = 0, j = this.options.offset;
wh = this.options.moduleWidth * Math.ceil(moduleCount / this.options.rows);
wh = wh + this.options.moduleWidth;
li.each(function() {
self.format(this, j++);
//wh += self.dimension(this, di);
});
this.list.css(this.wh, wh + 'px');
// Only set if not explicitly passed as option
if (!o || o.size === undefined) {
this.options.size = Math.ceil(li.size() / this.options.rows);
}
}
这是在下载 jscarousel 时使用代码包static_sample.html的调用:
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#mycarousel').jcarousel( {
scroll: 1,
moduleWidth: 75,
rows:2,
animation: 'slow'
});
});
</script>
如果您需要更改轮播的内容并重新加载轮播,则需要执行以下操作:
// Destroy contents of wrapper
$('.wrapper *').remove();
// Create UL list
$('.wrapper').append('<ul id="carousellist"></ul>')
// Load your items into the carousellist
for (var i = 0; i < 10; i++)
{
$('#carouselist').append('<li>Item ' + i + '</li>');
}
// Now apply carousel to list
jQuery('#carousellist').jcarousel({ // your config });
轮播 html 定义需要如下所示:
<div class="wrapper">
<ul id="mycarousel0" class="jcarousel-skin-tango">
...<li></li>
</ul>
</div>
感谢Webcidentes
我们不得不进行类似的修改。 我们通过扩展默认选项来做到这一点,以包括行值,每个项目的宽度(我们称之为模块),然后将宽度除以行数。
添加到 jCarousel 函数的代码...
添加到默认选项:
moduleWidth: null,
rows:null,
然后在创建 jCarousel 时设置:
$('.columns2.rows2 .mycarousel').jcarousel( {
scroll: 1,
moduleWidth: 290,
rows:2,
itemLoadCallback: tonyTest,
animation: 'slow'
});
查找和编辑以下行:
$.jcarousel = function(e, o) {
if (li.size() > 0) {
...
moduleCount = li.size();
wh = this.options.moduleWidth * Math.ceil( moduleCount / this.options.rows );
wh = wh + this.options.moduleWidth;
this.list.css(this.wh, wh + 'px');
// Only set if not explicitly passed as option
if (!o || o.size === undefined)
this.options.size = Math.ceil( li.size() / this.options.rows );
希望这有帮助,
托尼·狄龙
你可能想看看serialScroll或localScroll而不是jcarousel。
我在Google网上论坛上找到了这篇文章,该帖子具有多行的工作版本。 我已经用过这个,效果很好。http://groups.google.com/group/jquery-en/browse_thread/thread/2c7c4a86d19cadf9
我尝试了上述解决方案,发现更改原始 jCarousel 代码很麻烦 - 它为我引入了错误行为,因为它不能很好地与 jCarousel 的某些功能配合使用,例如连续循环等。
我使用了另一种效果很好的方法,我认为其他人也可以从中受益。这是我用来创建li项目的JS代码,以支持具有优雅项目流的多行jCarousel,即水平填充,然后垂直填充,然后滚动页面:
123 | 789
456 |0AB
它将(var carouselRows的值)项添加到单个li中,因此允许jCarousel支持多行而无需修改原始jCarousel代码。
// Populate Album photos with support for multiple rows filling first columns, then rows, then pages
var carouselRows=3; // number of rows in the carousel
var carouselColumns=5 // number of columns per carousel page
var numItems=25; // the total number of items to display in jcarousel
for (var indexpage=0; indexpage<Math.ceil(numItems/(carouselRows*carouselColumns)); indexpage++) // for each carousel page
{
for (var indexcolumn = 0; indexcolumn<carouselColumns; indexcolumn++) // for each column on that carousel page
{
// handle cases with less columns than value of carouselColumns
if (indexcolumn<numItems-(indexpage*carouselRows*carouselColumns))
{
var li = document.createElement('li');
for (var indexrow = 0; indexrow < carouselRows; indexrow++) // for each row in that column
{
var indexitem = (indexpage*carouselRows*carouselColumns)+(indexrow*carouselColumns)+indexcolumn;
// handle cases where there is no item for the row below
if (indexitem<numItems)
{
var div = document.createElement('div'), img = document.createElement('img');
img.src = imagesArray[indexitem]; // replace this by your images source
div.appendChild(img);
li.appendChild(div);
}
}
$ul.append(li); // append to ul in the DOM
}
}
}
在此代码用 li 项填充 ul 后,应调用 jCarousel。
希望这对某人有所帮助。
乔纳森
如果您需要一个快速的解决方案来解决固定或一次性的需求,并且绝对不涉及更改可能不时更新的核心库代码,那么以下内容可能适合。 要将以下六个项目转换为轮播上的两行:
<div class="item">contents</div>
<div class="item">contents</div>
<div class="item">contents</div>
<div class="item">contents</div>
<div class="item">contents</div>
<div class="item">contents</div>
您可以使用一个小 JS 将div 包装成两个 LI 组,然后初始化轮播。 你的方案可能允许你在服务器上执行分组并不总是可能的。 显然,您可以将其扩展到所需的任何行数。
var $pArr = $('div.item');
var pArrLen = $pArr.length;
for (var i = 0;i < pArrLen;i+=2){
$pArr.filter(':eq('+i+'),:eq('+(i+1)+')').wrapAll('<li />');
};
- 没有找到相关文章