【问题标题】:how to implement 3d slider in angular 10如何在角度 10 中实现 3d 滑块
【发布时间】:2020-09-28 02:49:47
【问题描述】:

我对 Angular 很陌生,我想在 Angular 中创建一个像 this 这样的 3d 轮播滑块。我该如何解决这个问题? 请查看链接以更好地说明问题

function rotateLeft(){
  if(on == 0){
    $('.p_slider__item:nth-of-type(' + pos + ')').animate({'left':'200px'},200)
    $('.p_slider__item:nth-of-type(' + pos + ')').css('z-index','0')
    $('.p_slider__item:nth-of-type(' + pos2 + ')').animate({'left':'-200px'},200)
    setTimeout (function(){
      $('.p_slider__item:nth-of-type(' + pos2 + ')').css({'transform':'scale(0.6)','opacity':'0.8','webkit-filter':'blur(2px)','z-index':'1'});
      pos++;pos2++;pos3++;
      if(pos > 3){pos = 1}if(pos2 > 3){pos2 = 1;}if(pos3 > 3){pos3 = 1;}
    },400)
    $('.p_slider__item:nth-of-type(' + pos3 + ')').animate({'left':'0px'},200)
    $('.p_slider__item:nth-of-type(' + pos3 + ')').css({'transform':'scale(1)','opacity':'1','webkit-filter':'blur(0px)','z-index':'2'})
    setTimeout (function(){
      on = 0; // Accept clicks again
    },time)
  }
}

【问题讨论】:

  • 首先,不要在 Angular 中使用 jquery,不要自己操作 DOM,这不是个好主意。使用 Angular 给你的东西,它会好 10 倍,而且更容易。
  • 谢谢。我正在寻找一种将链接的 jquery 代码转换为有角度接受的结构的方法
  • 为你找到了这个,但它是 AngularJS:openbase.io/js/angular-carousel

标签: angular


【解决方案1】:

google 是我们的朋友,在this David DeSandro's article 你拥有所有的“成分”。是的,是 css 和 javascript,但您可以轻松转换为 Angular

我们使用 viewChild 和 ViewChildren 来获取单元格

  @ViewChild('carousel') carousel:ElementRef
  @ViewChildren('carousel__cell') cells:QueryList<ElementRef>

我们需要一些辅助变量:

  selectedIndex = 0;
  cellWidth:number;
  cellHeight:number;
  isHorizontal:boolean = true;
  rotateFn = this.isHorizontal ? 'rotateY' : 'rotateX';
  radius:number
  theta:number;

  get cellCount()
  {
    return this.cells.length;
  }

现在创建一个函数 initCarousel

  initCarousel() {
    this.theta = 360 / this.cellCount;
    const cellSize = this.isHorizontal ? this.cellWidth : this.cellHeight;
    this.radius = Math.round( ( cellSize / 2) / Math.tan( Math.PI / this.cellCount ) );
    this.cells.forEach((cell:ElementRef,i:number)=>
    {
       if (i<this.cellCount)
       {
           cell.nativeElement.style.opacity=1
           const cellAngle=this.theta*i;
           cell.nativeElement.style.transform = this.rotateFn + '(' + cellAngle + 'deg) translateZ(' + this.radius + 'px)';
       }
       else
       {
        cell.nativeElement.style.opacity = 0;
        cell.nativeElement.style.transform = 'none';

       }
    })
    this.rotateCarousel();
  }

我们在 ngAfterViewInit 中调用

  ngAfterViewInit()
  {
    this.cellWidth = this.carousel.nativeElement.offsetWidth;
    this.cellHeight = this.carousel.nativeElement.offsetHeight;
    this.initCarousel()
  }

你可以在stackblitz看到

更新问题确实是关于“pseudo-3d”的,而且更简单。我们需要应用变换一个比例和一个位置,所以,我们可以有这样的功能

  getStyle(index:number)
  {
    if (!this.cellCount)
       return null;
    const angle=(index-this.selectedIndex)*2*Math.PI/this.cellCount
    const scale=((75)+25*Math.cos(angle))/100
    return {
      left:-75+150*Math.sin(angle)+'px',
      transform:'scale('+scale+')',
      position:'absolute',
      "z-index":Math.floor(100*scale)
    }
  }

并应用于轮播

<div class="carousel" style="position:relative">
  <div *ngFor="let i of [0,1,2,3,4,5,6]" class="carousel__cell" 
     [ngStyle]="getStyle(i)">{{i}}
  </div>
</div>

Update 2 使用 Angular 动画。好吧,最后一个轮播看起来像是我们想要实现的。对于特定情况,我们可以,例如在位置 0,2 和 7 定义了 9 个位置和 3 个图像

在构造函数中注入 AnimationBuilder

  constructor(private builder: AnimationBuilder) {}

并定义为变量

  private player: AnimationPlayer;
  timer = 1000;
  animates = [0, 2, 7];

我们定义一个辅助变量

  movements = [
    { pos: 0, right: [1, 2], left: [8, 7] },
    { pos: 2, right: [3, 4, 5, 6, 7], left: [1, 0] },
    { pos: 7, right: [8, 0], left: [6, 5, 4, 3, 2] }
  ];

这个想法是手动创建动画,所以如果例如一个元素在 pos=0 中,我们点击“右”我们创建 2 个动画以到达位置 1,2

是一些复杂的函数,我放了cmets

  animateViews(direction: string) {
    //with each element of the array [0,2,7]
    this.animates.forEach((x: number, index: number) => {
      //get the item and the mov
      const mov = this.movements.find(m => m.pos == x);
      const item = this.itemsView.find((_x, i) => i == index);

      //create an array of animations
        const animations = mov[direction].map(m => {
          const angle = (m * 2 * Math.PI) / 9;
          const scale = (75 + 25 * Math.cos(angle)) / 100;
          const applystyle = {
            left: -75 + 150 * Math.sin(angle) + "px",
            transform: "scale(" + scale + ")",
            position: "absolute",
            "z-index": Math.floor(100 * scale)
          };
          return animate(
            this.timer / mov[direction].length + "ms",
            style(applystyle)
          );
        });

        //we create the animation with builder.build
        const myAnimation = this.builder.build(animations);

        //the animation is applied to the element
        this.player = myAnimation.create(item.nativeElement);

        //when finished, change the value of the array [0,2,7]
        this.player.onDone(
          () =>
            (this.animates[index] = mov[direction][mov[direction].length - 1])
        );
        this.player.play();
    });
  }

new stackblitz

更新 3*:要理解公式,请拿纸和铅笔并使用一些数学:想象一个半径为“r”的圆周。从中心画一条垂直线并在圆周上标记一个点。将“角度”称为从垂直到连接点与圆周中心的直线顺时针方向的角度。

left=r*Math.sin(angle)-width_of_img/2

对于比例,我们需要记住,当“角度”为 0 时,比例为 1,当“角度”为 180 度时,比例为 0.5(或 1 和 0 之间的另一个数字 - 越少,深度越大)。将此值称为“minScale”。这意味着 Math.cos(0)=1 比例为 1,Math.cos(180degrees)=-1 比例为 minScale。所以

 scale=(1+minScale)/2+(1-minScale)/2*Math.cos(angle)

【讨论】:

    【解决方案2】:

    由于您是 Angular 新手,我建议您使用 3rd-party 组件的方法:

    1. 搜索适合您需要的 Angular 组件或具有帮助您构建组件的工具的 Angular 框架。
    2. 将其导入您的项目(使用npm install ...)。通常,几乎第 3 方组件将指导您如何自行安装。
    3. 将已安装的组件导入项目的.module.ts 文件,然后使用它。

    【讨论】:

    • 你能告诉我更多吗?提前致谢。
    【解决方案3】:

    这可能对你有帮助

    https://github.com/Wlada/angular-carousel-3d
    

    如果您使用 bower,只需运行 bower install angular-carousel-3d。如果没有,请从以下 github repos 下载文件 angular-swipe.js、carousel-3d.css 和 carousel3d.js:

    Angular Swipe
    Angular Carousel 3D
    

    注意:您也可以下载和使用缩小版。

    将这些文件添加到您的代码中:

    <link href="carousel-3d.css" rel="stylesheet" type="text/css" />
    <script src="angular.js"></script>
    <script src="angular-swipe.js"></script>
    <script src="carousel-3d.js"></script>
    

    将 angular-carousel-3d 模块作为依赖项添加到您的应用程序模块中:

    angular.module('MyApp', ['angular-carousel-3d']);
    

    在您的模板中: 将 carousel-3d 属性添加到任何元素。 使用您的对象数组添加 ng-model 添加 carousel3d-slide 指令和 ng-repeat 来渲染幻灯片

    <div carousel3d ng-model="arrayOfObjects">
        <div carousel3d-slide ng-repeat="slide in app.slides track by $index">
            <figure>
                <img ng-src="{{slide.src}}" alt=""/>
                <figcaption ng-bind="slide.caption"></figcaption>
            </figure>
        </div>
    </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-18
      • 1970-01-01
      • 2015-09-19
      • 1970-01-01
      相关资源
      最近更新 更多