abcdGJJ
V2EX  ›  问与答

求指点~ translate 实现图片无缝轮播

  •  
  •   abcdGJJ · Oct 21, 2016 · 5483 views
    This topic created in 3664 days ago, the information mentioned may be changed or developed.

    当前思路:在父 div 设置 translate(0, 0),然后每次切换时更改 X 坐标,即 tanslate(-600px, 0)

    问题:当切换到最后一张图片时,再点击切换时我想要回到第一张图,然后设置成 translate(0, 0),但是动画的方向却反了,有没有什么办法能实现方向不变的切换?

    10 replies  •  2019-03-19 16:03:26 +08:00
    sherlocktheplant
    sherlocktheplant
    Oct 21, 2016
    1
    始终只显示当前屏幕能够容纳的图片数量 N + 2 的数量的图片 当最左边的一张图片彻底不可见时则将该标签移动到所有标签的最后一个 并且把 URL 设置成即将进入视野(但还不可见)的图片的 URL

    当滑动到最后一张的时候 所有逻辑都一样 只是即将进入的那张图片变成了第一张而已
    abcdGJJ
    abcdGJJ
    OP
    Oct 21, 2016
    2
    @sherlocktheplant 改变 URL ?有没有通过改变 css 来实现的方法?

    这是我当前的代码:

    ```
    <ul class="photo" id="photo">
    <li><img src="imgs/5.jpg" alt=""></li>
    <li><img src="imgs/1.jpg" alt=""></li>
    <li><img src="imgs/2.jpg" alt=""></li>
    <li><img src="imgs/3.jpg" alt=""></li>
    <li><img src="imgs/4.jpg" alt=""></li>
    <li><img src="imgs/5.jpg" alt=""></li>
    <li><img src="imgs/1.jpg" alt=""></li>
    </ul>
    <a href="javascript:;" id="prev" class="arrow">&lt;</a>
    <a href="javascript:;" id="next" class="arrow">&gt;</a>
    ```

    ```
    var photo = document.querySelector('#photo'),
    nextItem = document.querySelector('#next')
    index = 1
    var translate = function (dom, type, time) {
    dom.style.transform = type
    dom.style.transitionDuration = time + 's'

    }
    var animated = function (offset) {
    go = 'translate(' + offset*index + 'px, 0)'
    if(index == 6) {
    index = 1
    }
    console.log(go)
    translate(photo, go, 0.5)
    }
    var next = function () {
    index++
    animated(-600)
    }
    nextItem.onclick = next;
    ```
    sherlocktheplant
    sherlocktheplant
    Oct 22, 2016
    3
    @abcdGJJ 没有办法 因为你说的滚动逻辑不符合浏览器的渲染规则
    chemzqm
    chemzqm
    Oct 22, 2016
    4
    换实现方式,可参考苹果官网
    abcdGJJ
    abcdGJJ
    OP
    Oct 22, 2016 via Android
    5
    @sherlocktheplant 但是我看淘宝首页的轮播图可以实现,一直搞不懂原理
    abcdGJJ
    abcdGJJ
    OP
    Oct 22, 2016 via Android
    6
    @chemzqm 好的,我去看看
    GermXu
    GermXu
    Oct 22, 2016
    7
    @chemzqm 官网的轮播图怎么取消了, 这才几天, 又要换样式了?
    chemzqm
    chemzqm
    Oct 22, 2016
    8
    @GermXu 没办法,人家生产力过剩😂
    maxixi
    maxixi
    Jul 13, 2017
    9
    同遇到这个问题,楼主解决了吗?
    xsjyvi
    xsjyvi
    Mar 19, 2019
    10
    这个问题其实有点意思。
    1. 以前使用 relative,left 属性是可已解决的。
    2. 现在使用 translate 就不行了,初步断定是 translate 不会引起浏览器“重排”导致。
    3. 按阮一峰的说法就是 浏览器已经很智能了,会尽量把所有的变动集中在一起,排成一个队列,然后一次性执行,尽量避免多次重新渲染。
    4. 解决路径把最后的跳转逻辑放到 setTimeout,17, 或者使用 window.requestAnimationFrame() 方法。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1332 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 51ms · UTC 20:10 · PVG 04:10 · LAX 12:10 · JFK 15:10
    ♥ Do have faith in what you're doing.