2017-09-17 18:49:58 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @authors linteng (875482941@qq.com)
|
|
|
|
* @date 2017-09-07 19:53:18
|
|
|
|
*/
|
|
|
|
|
|
|
|
define(["yua", "text!./main.htm", "css!./main"], function(yua, tpl){
|
|
|
|
|
2017-09-30 15:03:52 +08:00
|
|
|
var dom;
|
2017-10-10 20:54:12 +08:00
|
|
|
var sliderNum = 0;
|
|
|
|
var skin = ['skin-0','skin-1','skin-2','skin-3']
|
2017-09-17 18:49:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [根据当前幻灯片索引获取填充底下按钮数据]
|
|
|
|
* @param {Object} vm [vm对象]
|
|
|
|
* @return {[Array]} [填充到按钮的数据]
|
|
|
|
*/
|
2017-10-10 20:54:12 +08:00
|
|
|
function getBtnList(vm, el){
|
|
|
|
if(el){
|
|
|
|
dom = el
|
|
|
|
}
|
|
|
|
vm.maxNum = Math.floor(dom.offsetWidth / 90)
|
2017-09-17 18:49:58 +08:00
|
|
|
var curr = vm.curr + 1
|
2017-09-27 01:05:17 +08:00
|
|
|
let res = []
|
2017-09-17 18:49:58 +08:00
|
|
|
if(!vm.preview)
|
2017-09-27 01:05:17 +08:00
|
|
|
res = vm.sliderList
|
2017-09-17 18:49:58 +08:00
|
|
|
|
|
|
|
if(vm.maxNum >= vm.sliderList.length){
|
2017-09-27 01:05:17 +08:00
|
|
|
res = vm.sliderList
|
2017-09-17 18:49:58 +08:00
|
|
|
}else{
|
|
|
|
if(curr > vm.maxNum){
|
2017-09-27 01:05:17 +08:00
|
|
|
res = vm.sliderList.slice(curr - vm.maxNum, curr)
|
2017-09-17 18:49:58 +08:00
|
|
|
}else if(curr <= vm.maxNum){
|
2017-09-27 01:05:17 +08:00
|
|
|
res = vm.sliderList.slice(0, vm.maxNum)
|
2017-09-17 18:49:58 +08:00
|
|
|
}
|
|
|
|
}
|
2017-09-27 01:05:17 +08:00
|
|
|
return res
|
2017-09-17 18:49:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [设置自动轮播]
|
|
|
|
* @param {[type]} vm [description]
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
|
|
|
function autoSlide(vm){
|
2017-09-27 01:05:17 +08:00
|
|
|
vm.timer = setTimeout(function(){
|
2017-09-17 18:49:58 +08:00
|
|
|
vm.$go(1)
|
2017-09-17 23:22:25 +08:00
|
|
|
autoSlide(vm)
|
2017-10-10 20:54:12 +08:00
|
|
|
}, vm.time <= 0 ? 3000 : vm.time)
|
2017-09-17 18:49:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return yua.component("sliders", {
|
|
|
|
$template: tpl,
|
|
|
|
$init: function(vm){
|
|
|
|
vm.$go = function(num){
|
|
|
|
vm.curr += num
|
|
|
|
if(vm.curr < 0){
|
|
|
|
vm.curr = vm.sliderList.length - 1
|
|
|
|
}else if(vm.curr > vm.sliderList.length - 1){
|
|
|
|
vm.curr = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vm.$jump = function(i){
|
|
|
|
var curr = vm.curr + 1
|
|
|
|
if(curr > vm.maxNum && vm.preview){
|
|
|
|
var distance = vm.maxNum - (i + 1)
|
|
|
|
vm.curr -= distance
|
|
|
|
}else{
|
|
|
|
vm.curr = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vm.$stopSlide = function(){
|
|
|
|
if(vm.autoSlide){
|
2017-09-27 01:05:17 +08:00
|
|
|
clearTimeout(vm.timer)
|
2017-09-17 18:49:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vm.$startSlide = function(){
|
|
|
|
if(vm.autoSlide)
|
2017-09-17 23:22:25 +08:00
|
|
|
autoSlide(vm)
|
|
|
|
}
|
|
|
|
|
|
|
|
vm.$setSliderList = function(list){
|
2017-09-27 01:05:17 +08:00
|
|
|
vm.sliderBtnList.removeAll()
|
|
|
|
vm.sliderList.pushArray(list)
|
2017-09-17 18:49:58 +08:00
|
|
|
}
|
|
|
|
|
2017-10-10 20:54:12 +08:00
|
|
|
vm.$onSuccess(vm)
|
|
|
|
},
|
|
|
|
$ready: function(vm, el){
|
|
|
|
vm.skin = skin[vm.skin]
|
|
|
|
vm.currWidth = (100 / vm.sliderList.length)
|
|
|
|
if(vm.autoSlide)
|
|
|
|
autoSlide(vm)
|
|
|
|
|
|
|
|
if(vm.preview){
|
|
|
|
vm.sliderBtnList.removeAll()
|
|
|
|
vm.sliderBtnList.pushArray(getBtnList(vm, el))
|
2017-09-27 19:27:31 +08:00
|
|
|
}
|
|
|
|
|
2017-09-17 18:49:58 +08:00
|
|
|
vm.$watch('curr', function(val, old) {
|
|
|
|
|
2017-10-10 20:54:12 +08:00
|
|
|
vm.animation = vm.sliderType > 2 ? 'translate(0, ' + (-vm.currWidth * val) + '%)' : 'translate(' + (-vm.currWidth * val) + '%, 0)'
|
2017-09-27 01:05:17 +08:00
|
|
|
if(vm.preview && vm.maxNum < vm.sliderList.length){
|
|
|
|
vm.sliderBtnList.removeAll()
|
2017-10-10 20:54:12 +08:00
|
|
|
vm.sliderBtnList.pushArray(getBtnList(vm, el))
|
2017-09-27 01:05:17 +08:00
|
|
|
}
|
2017-09-17 18:49:58 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
window.addEventListener('resize', function(){
|
|
|
|
|
2017-10-10 20:54:12 +08:00
|
|
|
vm.animation = vm.sliderType > 2 ? 'translate(0, ' + (-vm.currWidth * vm.curr) + '%)' : 'translate(' + (-vm.currWidth * vm.curr) + '%, 0)'
|
2017-09-27 01:05:17 +08:00
|
|
|
if(vm.preview && vm.maxNum < vm.sliderList.length){
|
|
|
|
vm.sliderBtnList.removeAll()
|
2017-10-10 20:54:12 +08:00
|
|
|
vm.sliderBtnList.pushArray(getBtnList(vm, el))
|
2017-09-27 01:05:17 +08:00
|
|
|
}
|
2017-09-17 18:49:58 +08:00
|
|
|
}, false)
|
|
|
|
|
2017-10-10 20:54:12 +08:00
|
|
|
if(vm.sliderType >= 3){
|
|
|
|
var now = 0
|
|
|
|
var mouseWheelEv = /Firefox/i.test(navigator.userAgent) ? "DOMMouseScroll": "mousewheel"
|
|
|
|
var direction = /Firefox/i.test(navigator.userAgent) ? "detail": "deltaY"
|
|
|
|
window.addEventListener(mouseWheelEv, function(ev){
|
|
|
|
if(Date.now() - now > 500 || now == 0){
|
|
|
|
|
|
|
|
if(ev[direction] > 0){
|
|
|
|
vm.$go(1)
|
|
|
|
}else{
|
|
|
|
vm.$go(-1)
|
|
|
|
}
|
|
|
|
now = Date.now()
|
|
|
|
}
|
|
|
|
}, false)
|
2017-09-27 01:05:17 +08:00
|
|
|
}
|
2017-09-17 18:49:58 +08:00
|
|
|
},
|
|
|
|
currWidth: 0,
|
2017-09-27 19:27:31 +08:00
|
|
|
animation: '',
|
2017-09-17 18:49:58 +08:00
|
|
|
curr: 0,
|
|
|
|
sliderBtnList: [],
|
2017-09-26 00:24:31 +08:00
|
|
|
maxNum: 0,
|
2017-09-27 01:05:17 +08:00
|
|
|
timer: yua.noop,
|
2017-09-27 19:27:31 +08:00
|
|
|
sliderType: 1,
|
2017-09-17 18:49:58 +08:00
|
|
|
|
|
|
|
sliderList: [],
|
|
|
|
autoSlide: '',
|
|
|
|
time: 3000,
|
2017-10-10 20:54:12 +08:00
|
|
|
preview: false,
|
|
|
|
skin: 0,
|
|
|
|
fullScreen: false,
|
2017-09-17 18:49:58 +08:00
|
|
|
|
2017-09-17 23:22:25 +08:00
|
|
|
$onSuccess: yua.noop,
|
|
|
|
$setSliderList: yua.noop,
|
2017-09-17 18:49:58 +08:00
|
|
|
$jump: yua.noop,
|
|
|
|
$stopSlide: yua.noop,
|
|
|
|
$startSlide: yua.noop,
|
|
|
|
$go: yua.noop,
|
|
|
|
})
|
|
|
|
|
2017-09-27 01:05:17 +08:00
|
|
|
yua.scan(dom)
|
2017-09-17 18:49:58 +08:00
|
|
|
|
2017-09-30 15:03:52 +08:00
|
|
|
})
|