Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

跑马灯在横向滑动时,初始化宽度存在问题 #59

Closed
nuonuoge opened this issue Oct 17, 2018 · 4 comments
Closed

跑马灯在横向滑动时,初始化宽度存在问题 #59

nuonuoge opened this issue Oct 17, 2018 · 4 comments
Assignees

Comments

@nuonuoge
Copy link
Collaborator

nuonuoge commented Oct 17, 2018

Version
0.9.5
Environment
win10
Reproduction link
https://pktsbn.run.stackblitz.io/
Steps to reproduce
https://pktsbn.run.stackblitz.io/
What is expected?
CarouselSlide高度正常
What is actually happening?
CarouselSlide在父元素没有ng动画时,宽度为0
Other
1

纵向滑动时,宽度是auto未出现问题

carouselInit函数中
v.width = this.vertical ? 'auto' : this._rationWidth - this.cellSpacing; 

横向滑动时,当父组件有ng的动画时,在afterviewinit后会自动插入class:'ng-star-inserted',dom发生了变化,会调用afterviewInit里面的callback函数,_rationWidth就有数值了,但是在父组件没有ng的动画,BrowserAnimationsModule不会给CarouselSlide增加class:ng-star-inserted,也就无法触发callback,然后this._rationWidth数值一直是0,v.width就是0了
2018-10-17_210943

carouselInit(items) {
    this.infinite = this.infinite === undefined ? true : this.infinite;
    this._nodeArr = items['_results'];
    this.dragging = this._nodeArr.length > 1 && this.dragging ? this.dragging : false;
    this.dragging = this.dragging ? this.dragging : false;
    if (this._nodeArr.length > 1) {
      this._lastIndex = this._nodeArr.length - 1;
      setTimeout(() => {
        this._nodeArr.forEach((v, index) => {
          v.width = this.vertical ? 'auto' : this._rationWidth - this.cellSpacing; 
          v.left = this.vertical ? 0 : index === this._lastIndex ? -this._rationWidth : index * this._rationWidth;
          v.top = this.vertical ? (index === this._lastIndex ? -this._rationWidth : index * this._rationWidth) : 0;
          v.margin = this.vertical ? `${this.cellSpacing / 2}px auto` : `auto ${this.cellSpacing / 2}px`;
        });
        this.startTimer();
      }, 0);
    } else if (this._nodeArr.length === 1) {
      setTimeout(() => {
        this._nodeArr.forEach((v, index) => {
          v.width = this._rationWidth - this.cellSpacing;
          v.left = 0;
          v.top = 0;
          v.margin = `auto ${this.cellSpacing / 2}px`;
        });
      }, 0);
    }
  }

方案:
给carousel的component装饰器加上动画好使,不过这个ng-star-inserted是ng5出现的,未来不知道会不会去掉,有一点风险

animations: [
    trigger('', []) // 利用动画增加的css类ng-star-inserted首次触发MutationObserver
  ]

或者在afterviewinit里给_rationWidth等变量做callback中那样的赋值

@BronzeCui
Copy link
Contributor

你好,https://pktsbn.run.stackblitz.io/ 这个链接我打不开,请问一下具体什么情况下“父元素没有ng动画”?

@nuonuoge
Copy link
Collaborator Author

nuonuoge commented Oct 18, 2018

我正准备按照动画这个方案提pr,从官方走马灯demo的基本用法点进stackblitz就能看到,图片没有加载出来

@nuonuoge
Copy link
Collaborator Author

是问什么场景不用ng动画吗?开发的时候某个懒加载的模块很有可能不会使用trigger,走马灯的宽度就没了

@nuonuoge
Copy link
Collaborator Author

已修复:#67

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants