/* dd full screen slider
* created: may 20th, 2015 by dynamicdrive.com
* visit http://www.dynamicdrive.com/ for full source code
*/
var ddfullscreenslider = (function($){
document.createelement('section')
document.createelement('article')
var defaults = {
addhash: true,
sliderstate: 'open', // reserved for future use
keycodenavigation: [40, 38], // keycode codes for down and up naivagion, respectively
transitionduration: '0.5s',
swipeconfig: {peekamount: 100, mousedrag: true},
onslide: function($slide, index){}
}
var transformprop = '' // variable to contain browser supported version of "transform" property
function supportstranslate3d(){ // based on http://stackoverflow.com/a/12621264/4360074
if (!window.getcomputedstyle) {
return false;
}
var $el = $('
').appendto(document.body)
var has3d
$el.css('transform', 'translate3d(1px,1px,1px)')
has3d = $el.css('transform')
var findtransformprop = $el.get(0).style.csstext.match(/(\w|\-)*transform/i) // get "-vendor-transform" portion of css text
transformprop = (findtransformprop)? findtransformprop[0] : 'transform'
$el.remove()
return (has3d !== undefined && has3d.length > 0 && has3d !== "none");
}
var $window = $(window)
var $root = $('html')
function ddfullscreenslider(setting){
var translatesupport = supportstranslate3d()
var s = $.extend({}, defaults, setting)
var $slider = $('#' + s.sliderid)
var $wrapper = $slider.find('div.slidewrapper').css({transitionduration: s.transitionduration})
var $slides = $()
var slideslength = 0
var $nav = $()
var thisslider = this
var selectedindx = 0
var hasharray = []
var mouseslidetimer
var swipeevts = {start:"", move:"", end:""}, swipestart = false, dy = 0, bounds = [,], scrollableclass = 'scrollable', bypassdrag = false
var initialrun= true // indicate whether this is first time slider has run. after page loads, variable becomes false
if (!translatesupport){
s.transitionduration = parsefloat(s.transitionduration) * 1000
}
/** function to call always after a slide is shown */
function onslidealways($slide, index){
if (!initialrun && s.addhash){
var newhash = $nav.find('li').eq(selectedindx).find('a:eq(0)').attr('href')
if (history.replacestate){
history.replacestate(null, null, newhash)
}
else{
window.location.hash = newhash
}
}
s.onslide($slide, index) // user defined event handler
}
function hashtoselectedslide(hash){
for (var i=0; i 0)
var targetindx = math.max(0, selectedindx-1)
if (targetindx != selectedindx)
thisslider.slideto(targetindx)
}
$slider.on('mousewheel', function(event){
cleartimeout(mouseslidetimer)
var deltay = event.deltay
mouseslidetimer = settimeout(function(){
mouseslide(deltay)
}, 100)
return false
})
$(document).on('keyup', function(e){
var targetindx = selectedindx
if (e.keycode == s.keycodenavigation[0]) // key to go to next slide
var targetindx = math.min(slideslength-1, selectedindx+1)
else if (e.keycode == s.keycodenavigation[1]) // key to go to previous slide
var targetindx = math.max(0, selectedindx-1)
if (targetindx != selectedindx)
thisslider.slideto(targetindx)
})
/** swipe/ mouse drag scroll related code **/
swipeevts.start = 'touchstart' + (s.swipeconfig.mousedrag? ' mousedown' : '')
swipeevts.move = 'touchmove.dragmove' + s.sliderid + (s.swipeconfig.mousedrag? ' mousemove.dragmove' + s.sliderid : '')
swipeevts.end = 'touchend' + (s.swipeconfig.mousedrag? ' mouseup' : '')
if (s.swipeconfig.mousedrag){
$wrapper.bind('click', function(e){
if ($wrapper.data('dy') != 0) // if dragging on belt instead of clicking on it
e.preventdefault() // prevent default action
})
}
$wrapper.bind(swipeevts.start, function(e){
bypassdrag = false
var e = (e.type.indexof('touch') != -1)? e.originalevent.changedtouches[0] : e
if ( isscrollable( $(e.target) ) ){ // if target is contained inside a "scrollable" element
bypassdrag = true
return
}
swipestart = true
var mousey = e.pagey
dy = 0 // reset swipe amount
if (translatesupport){
$wrapper.css({transition: 'none'})
}
var initialy = -$window.outerheight() * selectedindx
$wrapper.data({dy: dy})
$(document).bind(swipeevts.move, function(e){
if (bypassdrag){
return
}
var e = (e.type.indexof('touch') != '-1')? e.originalevent.changedtouches[0] : e
dy=e.pagey-mousey //distance to move horizontally
var newy=initialy+dy
newy = applybounds(newy, (dy < 0)? 'down' : 'up')
if (translatesupport){
$wrapper.css('transform', 'translate3d(0, ' + newy + 'px, 0)')
}
else{
$wrapper.css('top', newy)
}
$wrapper.data({dy: dy})
return false //cancel default drag action
})
if (e.type == "mousedown")
return false //cancel default drag action
})
$(document).bind(swipeevts.end, function(e){
$(document).unbind(swipeevts.move)
if (!swipestart || bypassdrag)
return
if (dy != 0){ // if actual swipe has occured
if (dy < 0 && dy < -s.swipeconfig.peekamount) // if swipe down and more than peek amount
var targetindx = math.min(slideslength-1, selectedindx+1)
else if (dy > 0 && dy > s.swipeconfig.peekamount) // if swipe up and more than peek amount
var targetindx = math.max(0, selectedindx-1)
else
targetindx = selectedindx
thisslider.slideto(targetindx)
if (e.type == "mouseup")
return false
}
swipestart = false
})
/** css3 transition ontransitionend event set up */
$wrapper.on('transitionend webkittransitionend', function(e){
if (/transform/i.test(e.originalevent.propertyname)){ // check event fired on "transform" prop
onslidealways($slides.eq(selectedindx), selectedindx)
}
})
/** public functions **/
this.reloadslides = function(){
$nav.remove()
hasharray = []
$nav = $('')
$slides = $('article.slide').each(function(i){
var $curslide = $(this)
var anchorval = $curslide.attr('id') || 'slide' + (i+1)
anchorval = "#" + anchorval
hasharray.push([anchorval, i])
var title = $curslide.data('title') || anchorval
var $navli = $('' + (i + 1) + '').appendto($nav)
var $navlink = $navli.find('a:eq(0)')
$navli.on('click touchstart', function(e){
thisslider.slideto(i)
})
})
$nav.on('click touchstart touchend', function(e){
var e = (e.type.indexof('touch') != '-1')? e.originalevent.changedtouches[0] : e
return false
}).appendto($slider)
slideslength = $slides.length
bounds = [0, $window.outerheight() * (slideslength-1)]
}
this.openslider = function(){
initialrun = false
bypassdrag = false
s.sliderstate = 'open'
$root.addclass('fssopen')
$slider.css('visibility', 'visible')
}
this.closeslider = function(){
s.sliderstate = 'close'
bypassdrag = true
$root.removeclass('fssopen')
$slider.css({visibility:'hidden'})
}
this.slideto = function(indx, noanimation){
var newy = $window.outerheight() * indx
$nav.find('li').eq(selectedindx).removeclass('selected')
$nav.find('li').eq(indx).addclass('selected')
selectedindx = indx
if (translatesupport){
$wrapper.css({transition: (noanimation)? 'none' : transformprop + ' ' + s.transitionduration})
$wrapper.css('transform', 'translate3d(0, -' + newy + 'px, 0)')
if (noanimation){ // run callback immediately
onslidealways($slides.eq(selectedindx), selectedindx)
}
}
else{
$wrapper.stop().animate({top: -newy}, (noanimation)? 0 : s.transitionduration, function(){
onslidealways($slides.eq(selectedindx), selectedindx)
})
}
}
/** initialize and show slider **/
this.reloadslides()
selectedindx = hashtoselectedslide(window.location.hash)
this.slideto(selectedindx, true)
if (s.sliderstate == 'open'){
this.openslider()
}
$(window).on('resize', function(){
thisslider.slideto(selectedindx, true)
bounds = [0, $window.outerheight() * (slideslength-1)]
})
} // end ddfullscreenslider def
return ddfullscreenslider
})(jquery)