ViewPager
A versatile ViewPager controller, featuring both horizontal and vertical layout, gestures and automatic sliding.
A versatile ViewPager controller, featuring both horizontal and vertical layout, gestures and automatic sliding.
Add the following line, preferably inside the head section of the HTML document
<script src="//zuixjs.github.io/zuix/js/zuix.min.js"></script>Add the data-ui-load attribute to the div container
Page 1
Page 2
Page 3
The ViewPager will re-arrange all elements stacked horizontally or vertically accordingly to the chosen layout mode.
Elements do not necessarily require to be sized full screen. They can have different sizes, in which case the ViewPager will focus the active element to the center of the view.
ViewPager controller made with zUIx.div containerdata-o-slide="true"
data-o-paging="true"
div containerdata-o-layout="vertical"
data-o-passive="false"
The layout of this site itself is also entirely based on the ViewPager.
data-ui-load="@lib/controllers/view_pager" constructorview_pager controller on the element.data-ui-context optionaldata-ui-options optionaldata-o-layout optionalhorizontal or vertical. The default value is horizontal.data-o-paging optionalfalse.data-o-slide optionalfalse.data-o-slide-interval optional750.data-o-passive optionaltrue.data-o-hold optionalfalse.data-o-hide (experimental) optionalfalse.A reference to a view_pager object can be obtained trough its context identifier which is assigned by adding the data-ui-context attribute to the ViewPager element and finally by using the zuix.context(...) method.
Since components are loaded asynchronously the zuix.context method may return null if called before the component is ready. So, when not sure if the component is ready, it is more safe to use the callback argument of the zuix.context method.
<div data-ui-context="my-view-pager"
data-ui-load="@lib/controllers/view_pager">
</div>
<script>
var viewPager;
zuix.context('my-view-pager', function(){
viewPager = this;
});
</script>Another way getting a safe reference to a component is by using the data-ui-options attribute.
<div data-ui-options="vp_options"
data-ui-load="@lib/controllers/view_pager">
</div>
<script>
var viewPager;
vp_options = {
ready: function(ctx) {
viewPager = this;
}
}
</script>Other option fields
var vp_options = {
autoSlide: true, // default `false`
enablePaging: true, // default `false`
slideInterval: 500, // default `750`
verticalLayout: true, // default `false`
holdTouch: true, // default `false`
passive: false, // default `true`
autoHide: true // default `false`
}// select page
viewPager.page(2);
// get element of a page (returns ZxQuery-wrapped element)
const pageFour = viewPager.get(3);
pageFour.find('a').attr('rel', 'no-opener');
// select next page
viewPager.next();
// select previous page
viewPager.prev();
// get auto-slide mode (true/false)
const slideMode = viewPager.slide();
// enable or pause auto-slide mode
viewPager.slide(true);
// get current layout mode ('horizontal' or 'vertical')
const layoutMode = viewPager.layout();
// change current layout mode
viewPager.layout('vertical');// page change event
viewPager.on('page:change', function(e, pageInfo) {
// new page number
console.log(pageInfo.in);
// old page number
console.log(pageInfo.out);
// ...
}).on('page:tap', function(e, pageNumber) {
console.log(pageNumber);
// ...
});To bind events we can also use the data-ui-options attribute.
<div data-ui-options="vp_options"
data-ui-load="@lib/controllers/view_pager">
</div>
<script>
var vp_options = {
on: {
'page:change': function (e, pageInfo) {
// ...
},
'page:tap': function (e, pageNumber) {
// ...
}
}
}
</script>Try yourself the ViewPager controller live on Glitch.