function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function () { oldonload(); func(); } } } addLoadEvent(banner); /* banner焦点图手指左右滑动效果 */ function banner() { var _isTouch = false;//标记是滑动触摸事件,还是点击事件 var _startX;//开始触摸的起始横坐标 var _moveX;//手指滑动的横坐标 var _isLeft;//手指滑动方向,向左还是向右 var _index = 0;//clas=active(即当前显示的图片)li的索引值 var _speed = 300;//手机滑动时图片完成滑动的时间(速度) var _width = $(window).width(); /* 设置滑动的距离超过屏幕宽度的1/3个宽度才会产生最终的动画效果 */ var _moveDistance = _width / 3; var _length = $('.banner .list li').length - 1; var _timeTask; /* 定时任务 */ var _time = 3000; /* 定时任务每隔_time时间执行一次 */ var _speed1 = 800; /* _speed1时间内执行完一次 */ /*根据banner图的数量生成小圆按钮的数量*/ var _bar = ''; $('.banner').append(_bar); //判断是否支持触摸事件 function isSupportTouchEvent() { var isSupportTouch = "ontouchend" in document ? true : false; if (isSupportTouch) { //绑定触摸事件 document.getElementsByClassName('banner')[0].addEventListener('touchstart', touchStartFunc, false); document.getElementsByClassName('banner')[0].addEventListener('touchmove', touchMoveFunc, false); document.getElementsByClassName('banner')[0].addEventListener('touchend', touchEndFunc, false); } } //touchstart事件 function touchStartFunc(event) { clearInterval(_timeTask); var _touch = event.targetTouches[0];//获取第一个触点 _startX = _touch.pageX;//第一个触点的横坐标 } //touchmove事件标 function touchMoveFunc(event) { clearInterval(_timeTask); var _touch = event.targetTouches[0]; _moveX = _touch.pageX; _isLeft = _startX - _moveX; /*手指滑动的距离*/ _index = $('.banner .list .active').index(); _isTouch = true; if (_isLeft > 0) {/* 如果向左滑动 */ /* _index != _length,判断是否循环到最后一张图片:如果没有,则下一张就是下一张,继续循环;如果是,则下一张就是第一张,从头开始循环*/ _index = _index != _length ? ++_index : 0; $('.banner .list .active').css({'left': -_isLeft}); $('.banner .list li').eq(_index).css({'left': '100%'}).css({'left': _width - _isLeft}); } else if (_isLeft < 0) {/* 如果向右滑动 */ /* _index != 0,判断是否循环到第一张图片:如果没有,则上一张就是上一张,继续循环;如果是,则上一张就是最后一张,跳到最后开始循环*/ _index = _index != 0 ? --_index : _length; $('.banner .list .active').css({'left': -_isLeft}); $('.banner .list li').eq(_index).css({'left': '-100%'}).css({'left': -_width - _isLeft}); } event.preventDefault(); //阻止触摸时浏览器的缩放、滚动条滚动等 } //touchend事件 function touchEndFunc(event) { if (_isTouch) { var isAnimate = $('.banner .list li').is(':animated'); /* is(":animated")返回bool值,表示对象是否是动画元素(即li是否处于动画状态) */ if (!isAnimate) {//只有在li处于非动画状态时,才能继续下一次动画(滑动) /* 如果向左滑动 */ if (_isLeft > 0) { /* 如果滑动的距离超过屏幕宽度的_moveDistance,则继续向前滑动 */ /*Math.abs()方法表示取绝对值*/ if (Math.abs(_isLeft) > _moveDistance) { //手指离开屏幕以后,图片自动把未完成的状态继续完成 $('.banner .list .active').animate({'left': '-100%'}, _speed, function () { $(this).removeClass('active'); }); $('.banner .list li').eq(_index).animate({'left': 0}, _speed, function () { $(this).addClass('active'); }); $('.banner .bar li').eq(_index).addClass('active').siblings().removeClass('active'); $('.banner .title li').eq(_index).addClass('active').siblings().removeClass('active'); } else {/* 如果滑动的距离没有超过_moveDistance,则回到原点 */ $('.banner .list .active').animate({'left': 0}, _speed); $('.banner .list li').eq(_index).animate({'left': '100%'}); } } else {/* 如果向右滑动 */ //手指离开屏幕以后,图片自动把未完成的状态继续完成 if (Math.abs(_isLeft) > _moveDistance) { //手指离开屏幕以后,图片自动把未完成的状态继续完完成 $('.banner .list .active').animate({'left': '100%'}, _speed, function () { $(this).removeClass('active'); }); $('.banner .list li').eq(_index).animate({'left': 0}, _speed, function () { $(this).addClass('active'); }); $('.banner .bar li').eq(_index).addClass('active').siblings().removeClass('active'); $('.banner .title .li').eq(_index).addClass('active').siblings().removeClass('active'); } else {/* 如果滑动的距离没有超过_moveDistance,则回到原点 */ $('.banner .list .active').animate({'left': 0}, _speed); $('.banner .list li').eq(_index).animate({'left': '-100%'}); } } } _isTouch = false; } _timeTask = setInterval(_funcTask, _time); }// end touchend事件 isSupportTouchEvent(); _timeTask = setInterval(_funcTask, _time); function _funcTask() { _index = _index != _length ? ++_index : 0; $('.banner .list .active').animate({'left': '-100%'}, _speed1, function () { $(this).removeClass('active'); }); $('.banner .list li').eq(_index).css({'left': '100%'}).animate({'left': 0}, _speed1, function () { $(this).addClass('active'); }); $('.banner .bar li').eq(_index).addClass('active').siblings().removeClass('active'); $('.banner .title li').eq(_index).addClass('active').siblings().removeClass('active'); } } //鼠标移入事件 var dd_a = $('#dd>a'); var dt_a = $('#dt>a'); dd_a.each(function(i){ dd_a.mouseover(function(){ dd_a.removeClass('active'); $(this).addClass('active'); dt_a.css('display','none'); dt_a[$(this).index()].style.display = 'block'; }) })