今天看大神张鑫旭的文章,有一篇 基于CSS3 column多栏布局实现水平滑页翻页交互,
为熟悉column多栏布局写法,模仿 起点中文网 封装了个jquery拓展方法。如下:
css
html,
body {
width: 100%;
height: 100%;
overflow: hidden;
background: #C4B399;
}
html,
body,
ul,
li {
padding: 0;
margin: 0;
}
ul {
-webkit-columns: calc(100vw - 32px) 1;
-webkit-column-gap: 16px;
columns: calc(100vw - 32px) 1;
column-gap: 16px;
overflow: visible;
height: 100%;
transition: all 0.2s;
}
ul li {
list-style: none;
padding: 1em;
margin: 1em;
-webkit-column-break-inside: avoid;
page-break-inside: avoid;
background: #FFFFFF;
break-inside: avoid;
}
html
<ul></ul>
<div style="display: flex;justify-content: space-between;position: absolute;bottom: 0;width: 100%;">
<button>前一页</button>
<button>后一页</button>
</div>
js
依赖:jquery.js
(function () {
$.extend({
pageColumns: {
init: function (options) {
var that = this;
options = options || {};
//默认参数
that.el = options.el || $('ul');
that.prevBtn = options.prevBtn || $('button').eq(0);
that.nextBtn = options.nextBtn || $('button').eq(1);
that.columnGap = options.columnGap || 16;
that.currentPage = options.currentPage || 0;
that.sectionIndex = options.sectionIndex || 0;
that.direction = options.direction || 1;
that.totalWidth = that.el[0].scrollWidth;
that.pageWidth = that.el.outerWidth();
that.page = that.totalWidth / (that.pageWidth + that.columnGap * 2);
//前一页
that.prevBtn.on('click', function () {
if (that.currentPage) {
that.currentPage--;
that.el.css('transform', 'translateX(-' + (that.currentPage * (that.pageWidth + that.columnGap)) + 'px)');
return;
}
that.direction = 0;
that.sectionIndex--;
that.filContent();
that.slidePosition();
});
//后一页
that.nextBtn.on('click', function () {
if (that.currentPage < that.page - 1) {
that.currentPage++;
that.el.css('transform', 'translateX(-' + (that.currentPage * (that.pageWidth + that.columnGap)) + 'px)');
return;
}
that.direction = 1;
that.sectionIndex++;
that.filContent();
that.slidePosition();
});
//初始化
that.filContent();
that.slidePosition();
},
filContent: function () {
var templete = '很长很长的内容......';
var repeatText = '<li>' + templete.repeat(3) + '</li>';
var title = '<li>第' + this.sectionIndex + '章</li>';
this.el.html(title + repeatText.repeat(30));
},
slidePosition: function () {
var that = this;
that.el.css('transition', 'unset');
that.totalWidth = that.el[0].scrollWidth;
that.page = that.totalWidth / (that.pageWidth + that.columnGap * 2);
if (that.direction) {
that.currentPage = 0;
} else {
that.currentPage = Math.ceil(that.page - 1);
}
that.el.css('transform', 'translateX(-' + (that.currentPage * (that.pageWidth + that.columnGap)) + 'px)');
setTimeout(function () {
that.el.css('transition', 'all 0.2s');
}, 200);
}
}
});
})();
示例:请点这里