基于Jquery和html5实现炫酷的3D焦点图动画


Posted in Javascript onMarch 02, 2016

这款HTML5焦点图不仅可以手动点击按钮切换图片,而且还支持自动切换图片,使用起来也相当方便。如果你需要在网站中展示产品图片,那么这款焦点图插件非常适合你。

基于Jquery和html5实现炫酷的3D焦点图动画

在线演示源码下载

HTML代码

<section id="dg-container" class="dg-container">
 <div class="dg-wrapper">
 <a href="#"><img src="images/1.jpg" alt="image01"><div>http://www.colazionedamichy.it/</div></a>
 <a href="#"><img src="images/2.jpg" alt="image02"><div>http://www.percivalclo.com/</div></a>
 <a href="#"><img src="images/3.jpg" alt="image03"><div>http://www.wanda.net/fr</div></a>
 <a href="#"><img src="images/4.jpg" alt="image04"><div>http://lifeingreenville.com/</div></a>
 <a href="#"><img src="images/5.jpg" alt="image05"><div>http://circlemeetups.com/</div></a>
 <a href="#"><img src="images/6.jpg" alt="image06"><div>http://www.castirondesign.com/</div></a>
 <a href="#"><img src="images/7.jpg" alt="image07"><div>http://www.foundrycollective.com/</div></a>
 <a href="#"><img src="images/8.jpg" alt="image08"><div>http://www.mathiassterner.com/home</div></a>
 <a href="#"><img src="images/9.jpg" alt="image09"><div>http://learnlakenona.com/</div></a>
 <a href="#"><img src="images/10.jpg" alt="image10"><div>http://www.neighborhood-studio.com/</div></a>
 <a href="#"><img src="images/11.jpg" alt="image11"><div>http://www.beckindesign.com/</div></a>
 <a href="#"><img src="images/12.jpg" alt="image12"><div>http://kicksend.com/</div></a>
 </div>
 <nav> 
 <span class="dg-prev"><</span>
 <span class="dg-next">></span>
 </nav>
</section>

CSS代码

.dg-container{
 width: 100%;
 height: 450px;
 position: relative;
}
.dg-wrapper{
 width: 481px;
 height: 316px;
 margin: 0 auto;
 position: relative;
 -webkit-transform-style: preserve-3d;
 -moz-transform-style: preserve-3d;
 -o-transform-style: preserve-3d;
 -ms-transform-style: preserve-3d;
 transform-style: preserve-3d;
 -webkit-perspective: 1000px;
 -moz-perspective: 1000px;
 -o-perspective: 1000px;
 -ms-perspective: 1000px;
 perspective: 1000px;
}
.dg-wrapper a{
 width: 482px;
 height: 316px;
 display: block;
 position: absolute;
 left: 0;
 top: 0;
 background: transparent url(../images/browser.png) no-repeat top left;
 box-shadow: 0px 10px 20px rgba(0,0,0,0.3);
}
.dg-wrapper a.dg-transition{
 -webkit-transition: all 0.5s ease-in-out;
 -moz-transition: all 0.5s ease-in-out;
 -o-transition: all 0.5s ease-in-out;
 -ms-transition: all 0.5s ease-in-out;
 transition: all 0.5s ease-in-out;
}
.dg-wrapper a img{
 display: block;
 padding: 41px 0px 0px 1px;
}
.dg-wrapper a div{
 font-style: italic;
 text-align: center;
 line-height: 50px;
 text-shadow: 1px 1px 1px rgba(255,255,255,0.5);
 color: #333;
 font-size: 16px;
 width: 100%;
 bottom: -55px;
 display: none;
 position: absolute;
}
.dg-wrapper a.dg-center div{
 display: block;
}
.dg-container nav{
 width: 58px;
 position: absolute;
 z-index: 1000;
 bottom: 40px;
 left: 50%;
 margin-left: -29px;
}
.dg-container nav span{
 text-indent: -9000px;
 float: left;
 cursor:pointer;
 width: 24px;
 height: 25px;
 opacity: 0.8;
 background: transparent url(../images/arrows.png) no-repeat top left;
}
.dg-container nav span:hover{
 opacity: 1;
}
.dg-container nav span.dg-next{
 background-position: top right;
 margin-left: 10px;
}

JavaScript代码

/**
 * jquery.gallery.js
 * Copyright 2011, Pedro Botelho / Codrops
 * Free to use under the MIT license.
 *
 * Date: Mon Jan 30 2012
 */

(function( $, undefined ) {

 /*
 * Gallery object.
 */
 $.Gallery   = function( options, element ) {

 this.$el = $( element );
 this._init( options );

 };

 $.Gallery.defaults  = {
 current : 0, // index of current item
 autoplay : false,// slideshow on / off
 interval : 2000 // time between transitions
  };

 $.Gallery.prototype = {
 _init   : function( options ) {

  this.options  = $.extend( true, {}, $.Gallery.defaults, options );

  // support for 3d / 2d transforms and transitions
  this.support3d = Modernizr.csstransforms3d;
  this.support2d = Modernizr.csstransforms;
  this.supportTrans = Modernizr.csstransitions;

  this.$wrapper = this.$el.find('.dg-wrapper');

  this.$items  = this.$wrapper.children();
  this.itemsCount = this.$items.length;

  this.$nav  = this.$el.find('nav');
  this.$navPrev = this.$nav.find('.dg-prev');
  this.$navNext = this.$nav.find('.dg-next');

  // minimum of 3 items
  if( this.itemsCount < 3 ) {

  this.$nav.remove();
  return false;

  } 

  this.current = this.options.current;

  this.isAnim  = false;

  this.$items.css({
  'opacity' : 0,
  'visibility': 'hidden'
  });

  this._validate();

  this._layout();

  // load the events
  this._loadEvents();

  // slideshow
  if( this.options.autoplay ) {

  this._startSlideshow();

  }

 },
 _validate  : function() {

  if( this.options.current < 0 || this.options.current > this.itemsCount - 1 ) {

  this.current = 0;

  } 

 },
 _layout  : function() {

  // current, left and right items
  this._setItems();

  // current item is not changed
  // left and right one are rotated and translated
  var leftCSS, rightCSS, currentCSS;

  if( this.support3d && this.supportTrans ) {

  leftCSS = {
   '-webkit-transform' : 'translateX(-350px) translateZ(-200px) rotateY(45deg)',
   '-moz-transform' : 'translateX(-350px) translateZ(-200px) rotateY(45deg)',
   '-o-transform' : 'translateX(-350px) translateZ(-200px) rotateY(45deg)',
   '-ms-transform' : 'translateX(-350px) translateZ(-200px) rotateY(45deg)',
   'transform'  : 'translateX(-350px) translateZ(-200px) rotateY(45deg)'
  };

  rightCSS = {
   '-webkit-transform' : 'translateX(350px) translateZ(-200px) rotateY(-45deg)',
   '-moz-transform' : 'translateX(350px) translateZ(-200px) rotateY(-45deg)',
   '-o-transform' : 'translateX(350px) translateZ(-200px) rotateY(-45deg)',
   '-ms-transform' : 'translateX(350px) translateZ(-200px) rotateY(-45deg)',
   'transform'  : 'translateX(350px) translateZ(-200px) rotateY(-45deg)'
  };

  leftCSS.opacity = 1;
  leftCSS.visibility = 'visible';
  rightCSS.opacity = 1;
  rightCSS.visibility = 'visible';

  }
  else if( this.support2d && this.supportTrans ) {

  leftCSS = {
   '-webkit-transform' : 'translate(-350px) scale(0.8)',
   '-moz-transform' : 'translate(-350px) scale(0.8)',
   '-o-transform' : 'translate(-350px) scale(0.8)',
   '-ms-transform' : 'translate(-350px) scale(0.8)',
   'transform'  : 'translate(-350px) scale(0.8)'
  };

  rightCSS = {
   '-webkit-transform' : 'translate(350px) scale(0.8)',
   '-moz-transform' : 'translate(350px) scale(0.8)',
   '-o-transform' : 'translate(350px) scale(0.8)',
   '-ms-transform' : 'translate(350px) scale(0.8)',
   'transform'  : 'translate(350px) scale(0.8)'
  };

  currentCSS = {
   'z-index'  : 999
  };

  leftCSS.opacity = 1;
  leftCSS.visibility = 'visible';
  rightCSS.opacity = 1;
  rightCSS.visibility = 'visible';

  }

  this.$leftItm.css( leftCSS || {} );
  this.$rightItm.css( rightCSS || {} );

  this.$currentItm.css( currentCSS || {} ).css({
  'opacity' : 1,
  'visibility': 'visible'
  }).addClass('dg-center');

 },
 _setItems  : function() {

  this.$items.removeClass('dg-center');

  this.$currentItm = this.$items.eq( this.current );
  this.$leftItm = ( this.current === 0 ) ? this.$items.eq( this.itemsCount - 1 ) : this.$items.eq( this.current - 1 );
  this.$rightItm = ( this.current === this.itemsCount - 1 ) ? this.$items.eq( 0 ) : this.$items.eq( this.current + 1 );

  if( !this.support3d && this.support2d && this.supportTrans ) {

  this.$items.css( 'z-index', 1 );
  this.$currentItm.css( 'z-index', 999 );

  }

  // next & previous items
  if( this.itemsCount > 3 ) {

  // next item
  this.$nextItm = ( this.$rightItm.index() === this.itemsCount - 1 ) ? this.$items.eq( 0 ) : this.$rightItm.next();
  this.$nextItm.css( this._getCoordinates('outright') );

  // previous item
  this.$prevItm = ( this.$leftItm.index() === 0 ) ? this.$items.eq( this.itemsCount - 1 ) : this.$leftItm.prev();
  this.$prevItm.css( this._getCoordinates('outleft') );

  }

 },
 _loadEvents  : function() {

  var _self = this;

  this.$navPrev.on( 'click.gallery', function( event ) {

  if( _self.options.autoplay ) {

   clearTimeout( _self.slideshow );
   _self.options.autoplay = false;

  }

  _self._navigate('prev');
  return false;

  });

  this.$navNext.on( 'click.gallery', function( event ) {

  if( _self.options.autoplay ) {

   clearTimeout( _self.slideshow );
   _self.options.autoplay = false;

  }

  _self._navigate('next');
  return false;

  });

  this.$wrapper.on( 'webkitTransitionEnd.gallery transitionend.gallery OTransitionEnd.gallery', function( event ) {

  _self.$currentItm.addClass('dg-center');
  _self.$items.removeClass('dg-transition');
  _self.isAnim = false;

  });

 },
 _getCoordinates : function( position ) {

  if( this.support3d && this.supportTrans ) {

  switch( position ) {
   case 'outleft':
   return {
    '-webkit-transform' : 'translateX(-450px) translateZ(-300px) rotateY(45deg)',
    '-moz-transform' : 'translateX(-450px) translateZ(-300px) rotateY(45deg)',
    '-o-transform' : 'translateX(-450px) translateZ(-300px) rotateY(45deg)',
    '-ms-transform' : 'translateX(-450px) translateZ(-300px) rotateY(45deg)',
    'transform'  : 'translateX(-450px) translateZ(-300px) rotateY(45deg)',
    'opacity'  : 0,
    'visibility' : 'hidden'
   };
   break;
   case 'outright':
   return {
    '-webkit-transform' : 'translateX(450px) translateZ(-300px) rotateY(-45deg)',
    '-moz-transform' : 'translateX(450px) translateZ(-300px) rotateY(-45deg)',
    '-o-transform' : 'translateX(450px) translateZ(-300px) rotateY(-45deg)',
    '-ms-transform' : 'translateX(450px) translateZ(-300px) rotateY(-45deg)',
    'transform'  : 'translateX(450px) translateZ(-300px) rotateY(-45deg)',
    'opacity'  : 0,
    'visibility' : 'hidden'
   };
   break;
   case 'left':
   return {
    '-webkit-transform' : 'translateX(-350px) translateZ(-200px) rotateY(45deg)',
    '-moz-transform' : 'translateX(-350px) translateZ(-200px) rotateY(45deg)',
    '-o-transform' : 'translateX(-350px) translateZ(-200px) rotateY(45deg)',
    '-ms-transform' : 'translateX(-350px) translateZ(-200px) rotateY(45deg)',
    'transform'  : 'translateX(-350px) translateZ(-200px) rotateY(45deg)',
    'opacity'  : 1,
    'visibility' : 'visible'
   };
   break;
   case 'right':
   return {
    '-webkit-transform' : 'translateX(350px) translateZ(-200px) rotateY(-45deg)',
    '-moz-transform' : 'translateX(350px) translateZ(-200px) rotateY(-45deg)',
    '-o-transform' : 'translateX(350px) translateZ(-200px) rotateY(-45deg)',
    '-ms-transform' : 'translateX(350px) translateZ(-200px) rotateY(-45deg)',
    'transform'  : 'translateX(350px) translateZ(-200px) rotateY(-45deg)',
    'opacity'  : 1,
    'visibility' : 'visible'
   };
   break;
   case 'center':
   return {
    '-webkit-transform' : 'translateX(0px) translateZ(0px) rotateY(0deg)',
    '-moz-transform' : 'translateX(0px) translateZ(0px) rotateY(0deg)',
    '-o-transform' : 'translateX(0px) translateZ(0px) rotateY(0deg)',
    '-ms-transform' : 'translateX(0px) translateZ(0px) rotateY(0deg)',
    'transform'  : 'translateX(0px) translateZ(0px) rotateY(0deg)',
    'opacity'  : 1,
    'visibility' : 'visible'
   };
   break;
  };

  }
  else if( this.support2d && this.supportTrans ) {

  switch( position ) {
   case 'outleft':
   return {
    '-webkit-transform' : 'translate(-450px) scale(0.7)',
    '-moz-transform' : 'translate(-450px) scale(0.7)',
    '-o-transform' : 'translate(-450px) scale(0.7)',
    '-ms-transform' : 'translate(-450px) scale(0.7)',
    'transform'  : 'translate(-450px) scale(0.7)',
    'opacity'  : 0,
    'visibility' : 'hidden'
   };
   break;
   case 'outright':
   return {
    '-webkit-transform' : 'translate(450px) scale(0.7)',
    '-moz-transform' : 'translate(450px) scale(0.7)',
    '-o-transform' : 'translate(450px) scale(0.7)',
    '-ms-transform' : 'translate(450px) scale(0.7)',
    'transform'  : 'translate(450px) scale(0.7)',
    'opacity'  : 0,
    'visibility' : 'hidden'
   };
   break;
   case 'left':
   return {
    '-webkit-transform' : 'translate(-350px) scale(0.8)',
    '-moz-transform' : 'translate(-350px) scale(0.8)',
    '-o-transform' : 'translate(-350px) scale(0.8)',
    '-ms-transform' : 'translate(-350px) scale(0.8)',
    'transform'  : 'translate(-350px) scale(0.8)',
    'opacity'  : 1,
    'visibility' : 'visible'
   };
   break;
   case 'right':
   return {
    '-webkit-transform' : 'translate(350px) scale(0.8)',
    '-moz-transform' : 'translate(350px) scale(0.8)',
    '-o-transform' : 'translate(350px) scale(0.8)',
    '-ms-transform' : 'translate(350px) scale(0.8)',
    'transform'  : 'translate(350px) scale(0.8)',
    'opacity'  : 1,
    'visibility' : 'visible'
   };
   break;
   case 'center':
   return {
    '-webkit-transform' : 'translate(0px) scale(1)',
    '-moz-transform' : 'translate(0px) scale(1)',
    '-o-transform' : 'translate(0px) scale(1)',
    '-ms-transform' : 'translate(0px) scale(1)',
    'transform'  : 'translate(0px) scale(1)',
    'opacity'  : 1,
    'visibility' : 'visible'
   };
   break;
  };

  }
  else {

  switch( position ) {
   case 'outleft' : 
   case 'outright' : 
   case 'left' : 
   case 'right' :
   return {
    'opacity'  : 0,
    'visibility' : 'hidden'
   };
   break;
   case 'center' :
   return {
    'opacity'  : 1,
    'visibility' : 'visible'
   };
   break;
  };

  }

 },
 _navigate  : function( dir ) {

  if( this.supportTrans && this.isAnim )
  return false;

  this.isAnim = true;

  switch( dir ) {

  case 'next' :

   this.current = this.$rightItm.index();

   // current item moves left
   this.$currentItm.addClass('dg-transition').css( this._getCoordinates('left') );

   // right item moves to the center
   this.$rightItm.addClass('dg-transition').css( this._getCoordinates('center') ); 

   // next item moves to the right
   if( this.$nextItm ) {

   // left item moves out
   this.$leftItm.addClass('dg-transition').css( this._getCoordinates('outleft') );

   this.$nextItm.addClass('dg-transition').css( this._getCoordinates('right') );

   }
   else {

   // left item moves right
   this.$leftItm.addClass('dg-transition').css( this._getCoordinates('right') );

   }
   break;

  case 'prev' :

   this.current = this.$leftItm.index();

   // current item moves right
   this.$currentItm.addClass('dg-transition').css( this._getCoordinates('right') );

   // left item moves to the center
   this.$leftItm.addClass('dg-transition').css( this._getCoordinates('center') );

   // prev item moves to the left
   if( this.$prevItm ) {

   // right item moves out
   this.$rightItm.addClass('dg-transition').css( this._getCoordinates('outright') );

   this.$prevItm.addClass('dg-transition').css( this._getCoordinates('left') );

   }
   else {

   // right item moves left
   this.$rightItm.addClass('dg-transition').css( this._getCoordinates('left') );

   }
   break; 

  };

  this._setItems();

  if( !this.supportTrans )
  this.$currentItm.addClass('dg-center');

 },
 _startSlideshow : function() {

  var _self = this;

  this.slideshow = setTimeout( function() {

  _self._navigate( 'next' );

  if( _self.options.autoplay ) {

   _self._startSlideshow();

  }

  }, this.options.interval );

 },
 destroy  : function() {

  this.$navPrev.off('.gallery');
  this.$navNext.off('.gallery');
  this.$wrapper.off('.gallery');

 }
 };

 var logError  = function( message ) {
 if ( this.console ) {
  console.error( message );
 }
 };

 $.fn.gallery  = function( options ) {

 if ( typeof options === 'string' ) {

  var args = Array.prototype.slice.call( arguments, 1 );

  this.each(function() {

  var instance = $.data( this, 'gallery' );

  if ( !instance ) {
   logError( "cannot call methods on gallery prior to initialization; " +
   "attempted to call method '" + options + "'" );
   return;
  }

  if ( !$.isFunction( instance[options] ) || options.charAt(0) === "_" ) {
   logError( "no such method '" + options + "' for gallery instance" );
   return;
  }

  instance[ options ].apply( instance, args );

  });

 } 
 else {

  this.each(function() {

  var instance = $.data( this, 'gallery' );
  if ( !instance ) {
   $.data( this, 'gallery', new $.Gallery( options, this ) );
  }
  });

 }

 return this;

 };

})( jQuery );

希望本文所述对大家学习javascript程序设计有所帮助。

Javascript 相关文章推荐
Javascript var变量隐式声明方法
Oct 19 Javascript
15款优秀的jQuery导航菜单插件分享
Jul 19 Javascript
js中的数组Array定义与sort方法使用示例
Aug 29 Javascript
基于JQuery实现图片轮播效果(焦点图)
Feb 02 Javascript
AngularJS使用自定义指令替代ng-repeat的方法
Sep 17 Javascript
JavaScript Ajax实现异步通信
Dec 14 Javascript
JS图片轮播与索引变色功能实例详解
Jul 06 Javascript
JavaScript中正则表达式判断匹配规则及常用方法
Aug 03 Javascript
JS随机排序数组实现方法分析
Oct 11 Javascript
深入浅析Vue全局组件与局部组件的区别
Jun 15 Javascript
使用vue实现各类弹出框组件
Jul 03 Javascript
vue 将多个过滤器封装到一个文件中的代码详解
Sep 05 Javascript
JS判断是否长按某一键的方法
Mar 02 #Javascript
JS实现为排序好的字符串找出重复行的方法
Mar 02 #Javascript
在页面中输出当前客户端时间javascript实例代码
Mar 02 #Javascript
基于jQuery实现仿51job城市选择功能实例代码
Mar 02 #Javascript
基于JavaScript实现类似于百度学术高级检索功能
Mar 02 #Javascript
js密码强度实时检测代码
Mar 02 #Javascript
js+css实现回到顶部按钮(back to top)
Mar 02 #Javascript
You might like
php性能优化分析工具XDebug 大型网站调试工具
2011/05/22 PHP
PHP采集类snoopy详细介绍(snoopy使用教程)
2014/06/19 PHP
PHP实现多级分类生成树的方法示例
2017/02/07 PHP
JavaScript 存在陷阱 删除某一区域所有节点
2010/05/10 Javascript
jquery插件制作简单示例说明
2012/02/03 Javascript
jQuery.getScript加载同域JS的代码
2012/02/13 Javascript
input 输入框获得/失去焦点时隐藏/显示文字(jquery版)
2013/04/02 Javascript
jquery 通过name快速取值示例
2014/01/24 Javascript
JavaScript实现的内存数据库LokiJS介绍和入门实例
2014/11/17 Javascript
JavaScript中document对象使用详解
2015/01/06 Javascript
Javascript基础教程之关键字和保留字汇总
2015/01/18 Javascript
JavaScript处理解析JSON数据过程详解
2015/09/11 Javascript
关于Bootstrap弹出框无法调用问题的解决办法
2016/03/10 Javascript
JS实现回到页面顶部动画效果的简单实例
2016/05/24 Javascript
微信小程序 template模板详解及实例
2017/02/21 Javascript
iview form清除校验状态的实现
2019/09/19 Javascript
vue-cli打包后本地运行dist文件中的index.html操作
2020/08/12 Javascript
[00:58]他们到底在电话里听到了什么?
2017/11/21 DOTA
使用Python中的cookielib模拟登录网站
2015/04/09 Python
python实战之实现excel读取、统计、写入的示例讲解
2018/05/02 Python
Matplotlib 生成不同大小的subplots实例
2018/05/25 Python
深入浅析Python中list的复制及深拷贝与浅拷贝
2018/09/03 Python
Python字符串对象实现原理详解
2019/07/01 Python
Pycharm小白级简单使用教程
2020/01/08 Python
Python datetime 如何处理时区信息
2020/09/02 Python
H5页面适配iPhoneX(就是那么简单)
2019/12/02 HTML / CSS
中国领先的专业演出票务网:永乐票务
2016/08/29 全球购物
欧尚俄罗斯网上超市:Auchan俄罗斯
2018/05/03 全球购物
英国异国风情旅游网站:Travel Talk Tours(团体旅游、探险旅游、帆船假期)
2018/07/26 全球购物
某/etc/fstab文件中的某行如下: /dev/had5 /mnt/dosdata msdos defaults,usrquota 1 2 请解释其含义
2013/09/18 面试题
4s客服专员岗位职责
2013/12/01 职场文书
《小小竹排画中游》教学反思
2014/02/26 职场文书
计算机售后服务承诺书
2014/05/30 职场文书
我们的节日春节活动方案
2014/08/22 职场文书
公司车辆管理制度
2015/08/04 职场文书
青少年法制教育心得体会
2016/01/14 职场文书