Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="carousel_3d" class="carousel-3d">
  <div class="carousel-item"></div>
  <div class="carousel-item"></div>
  <div class="carousel-item"></div>
  <div class="carousel-item"></div>
</div>

              
            
!

CSS

              
                html
  width 100%
  height 100%
  background-image radial-gradient(circle, #000, #555)

.carousel-3d
  position relative
  width 20px
  height 20px
  background-color #2d4aff
  margin 200px auto
  
  .carousel-item
    position absolute
    left 50%
    top 50%
    width 300px
    height 200px
    margin-left -150px
    margin-top -100px
    background-size cover
    
  .carousel-item:nth-child(1)
      background-image url(http://news.cctvnews.cn/news/3d457a4d3149544d/img/73233fb6-0afe-41d7-900d-ae3e2d088313.jpg)
  
  .carousel-item:nth-child(2)
      background-image url(http://news.cctvnews.cn/news/3d55444e3249544d/img/ea65c16b-1a90-47ba-9997-46ddf0a7a32f.jpg)
      
  .carousel-item:nth-child(3)
      background-image url(http://news.cctvnews.cn/news/3d55444e3249544d/img/c91d9953-7b36-4da4-9174-7574b8239e2c.jpg)
      
  .carousel-item:nth-child(4)
      background-image url(http://news.cctvnews.cn/news/3d676a4d7949544d/img/b0c3e3c3-0161-4f15-bcd4-4380a3ca0bd2.jpg)
    
  for n in 1 2 3 4
    .carousel-item:nth-child({n})
      background-color darken(#D62828, (15*n))
      
              
            
!

JS

              
                /*
 * Author: Nokey
 * Date: 2016/12/06
 * Dependencies: jQuery, Tween.js
 */
'use strict';

class CarouselPoint{
  constructor(initState){
    var _me = this;
    // jQuery DOM
    this.dom = initState.dom;
    
    // TranslateX
    this.x = 0;
    
    // Scale()
    this.s = 0;
    
    // z-index
    this.z = 0;
    
    // 获取父组件属性
    this.A = initState.A;
    this.S = initState.S;
    this.Z = initState.Z;
    
    // 当前的总弧度
    this.rad = initState.rad;
    
    // 动画器
    this.tween = new TWEEN.Tween(_me)
                          .onStart(()=>{
                            _me.start();
                          })
                          .onUpdate(()=>{
                            _me.setState();
                          })
                          .onComplete(()=>{
                            _me.complete();
                          });
    
    // 动画时间
    this.time = initState.time;
  }
  
  setState(){ // theta 是每次增加或减少的弧度值
    // this.rad += theta;
    var _me = this;
    _me.x = _me.A * _me.checkMinNum(Math.cos(_me.rad));
    _me.s = _me.S * Math.sin(_me.rad) + 1;
    _me.z = parseInt(_me.Z * ((1 + _me.checkMinNum(Math.sin(_me.rad))) / 2));
    // Render
    _me.render();
    
    return _me;
  }
  
  checkMinNum(n){
    if(Math.abs(n) < 0.001){
      return 0;
    }
    return Math.floor(n * 1000) / 1000;
  }
  
  start(){
    console.log('This image Tween Start...');
    // 动画开始前取消 Focus 状态
    this.dom.removeClass('focus-3d');
  }
  
  complete(){
    console.log('This image Tween Complete...');
    // 检查当前元素是否在 Focus 位置
    if(this.x === 0 && this.z === this.Z){
      this.dom.addClass('focus-3d');
    }
  }
  
  animate(theta){
    var _me = this;
    _me.tween.stop().to({rad: _me.rad + theta}, _me.time).start();
  }
  
  render(){
    var _me = this;
    _me.dom.css({
      'z-index': _me.z,
      '-webkit-transform': 'scale('+_me.s+') translate('+_me.x+'px,0)',
      '-ms-transform': 'scale('+_me.s+') translate('+_me.x+'px,0)',
      'transform': 'scale('+_me.s+') translate('+_me.x+'px,0)'
    });
  }
}


class Carousel3D{
  constructor(initState){
    // a: 椭圆长轴;b: 椭圆短轴
    this.a = initState.a;
    this.b = initState.b;
    
    // DOM数组元素
    this.doms = initState.doms;
    
    // CarouselPoint 对象数组 [Array]
    this.CPs = [];
    
    // CarouselPoint 数量
    this.num = initState.doms.length;
    
    // 缩放可变范围 [Number] 1 ± s
    this.scaleDelta = initState.scaleDelta;
    
    // z-index 可变范围 [Number]
    this.zIndexMax = initState.zIndexMax;
    
    // 每个点相差的弧度
    this.deltaRad = Math.PI * 2 / this.num;
    
    // 动画时间
    this.time = initState.time;
    
    // 动画控制标志
    this.moving = false;
  }
  
  // 初始化控件
  init(){
    if(this.num < 1){
      throw 'There is no element to show!';
    }else{
      var _me = this;
      
      _me.doms.each((i, e)=>{
        var $e = $(e),
            o = new CarouselPoint({
              dom: $e,
              rad: (Math.PI / 2) + (_me.deltaRad * i),
              time: _me.time,
              A: _me.a,
              S: _me.scaleDelta,
              Z: _me.zIndexMax
            });
        _me.CPs.push(o);
        
        // 初始化 Focus
        i === 0 && $e.addClass('focus-3d');
        
        // 将dom映射到GP对象
        $e.data('gpnum', i);
        
        // 给每个dom绑定click事件
        $e.click((e)=>{
          // console.log($(e.target));
          // 确定要旋转的弧度
          var i = $(e.target).data('gpnum'),
              theta = _me.getNextTheta(i);

          // 开始 tween
          _me.animate(theta);
        });
      });
      
      _me.render();
      
      return _me;
    }
  }
  
  // 动画
  animate(theta){
    this.CPs.forEach((e)=>{
      e.animate(theta);
    });
  }
  
  // 获取即将变化的theta
  getNextTheta(i){
    var currentGP = this.CPs[i],
        diff_theta = (currentGP.rad % (Math.PI * 2)) - (Math.PI / 2),
        temp_theta = Math.abs(diff_theta) - Math.PI,
        next_theta;
    
    if(temp_theta >= 0){
      if(diff_theta >= 0){
        next_theta = Math.PI - temp_theta;
      }else{
        next_theta = -1*(Math.PI - temp_theta);
      }
    }else{
      next_theta = -1*diff_theta;
    }
    
    return next_theta;
  }
  
  // 初始化渲染每个点
  render(){
    if(this.num < 1){
      throw 'There is no element to update!';
    }else{
      this.CPs.forEach((e, i)=>{
        e.setState();
      });
      
      return this;
    }
  }
}

var C3D = new Carousel3D({
  a: 200,
  b: 20,
  doms: $('.carousel-item'),
  scaleDelta: 0.15,
  zIndexMax: 100,
  time: 500
}).init();

animate();

function animate() {
    requestAnimationFrame(animate);
  
    // Tween FPS
    TWEEN.update();
}

              
            
!
999px

Console