账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    图片向左滚动定位的问题?
    27
    0

    我想做一个类似跑马灯的抽奖,目前已经实现了图片轮播部分,代码如下:

    <template>
      <div>
        <div id="swiper">
            <div class="imgBox" ref="imgBox">
                <div class="imgDiv" ref="imgDiv" v-for="(item,index) of imgList" :key="index">
                    <img :src="item" />
                </div>
            </div>
        </div>
        <div @click="clickLeft">开始</div>
      </div>
    </template>
    <script>
    export default {
        data () {
            return {
                imgList:[
                    'http://n.sinaimg.cn/news/1_img/dfic/3ad618a7/107/w1024h683/20191023/46f6-ihfpfwa8114476.jpg',
                    'http://n.sinaimg.cn/news/1_img/dfic/3ad618a7/107/w1024h683/20191023/f798-ihfpfwa8114474.jpg',
                    'http://n.sinaimg.cn/news/1_img/dfic/3ad618a7/107/w1024h683/20191023/2e70-ihfpfwa8114472.jpg',
                    'http://n.sinaimg.cn/news/1_img/dfic/3ad618a7/107/w683h1024/20191023/92ee-ihfpfwa8114467.jpg'
                ],
                timer: null,
                theSpeed: 2,
                imgWidth: 260,
                theDirection: 'left',
            }
        },
        methods:{
          clickLeft () {
            // console.log(Math.floor(Math.random() * this.imgList.length))
            this.toLeft();
          },
          toLeft () {
            let that=this;
            let imgBox = this.$refs.imgBox;
            let imgDiv = this.$refs.imgDiv;
            imgBox.innerHTML += imgBox.innerHTML;
            imgBox.style.width = imgDiv.length * this.imgWidth + 'px'; // 设置ul的宽度使图片可以放下
            function autoScroll() {
              if (imgBox.offsetLeft < -(imgBox.offsetWidth/2)) { // 向左滚动
                imgBox.style.left = 0;
              }
              that.theSpeed = -Math.abs(that.theSpeed)
              imgBox.style.left = imgBox.offsetLeft + that.theSpeed + 'px';
            }
            this.timer = setInterval(autoScroll, 30); // 全局变量 ,保存返回的定时器
          }
        },
        mounted(){
        }
    }
    </script>
    <style scoped lang='less'>
    #swiper{
      position: relative;
      width: 750px;
      height: 176px;
      overflow: hidden;
      .imgBox{
        position:absolute;
        left:0;
        top:0;
        display: flex;
        .imgDiv{
          width: 100%;
          margin-left: 15px;
        }
      }
    }
    </style>

    但是我没有理清楚一个逻辑,由于现在是图片轮播,也就是说当第一组图片播完,图片列表重新回归到其实也就是0的位置,以达到视觉上的无缝轮播

    当我点击“开始”,如何才能使图片滚动到一个指定图片的位置呢?比如,我点击“开始”,从图片列表中随机到了2下标...然后开始滚动数圈,最后下标为2的图停止在了滚动可视区左侧。

    还望指点迷津,谢谢!

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    • 你不懂 普通会员 1楼

      图片向左滚动定位的问题,可以通过以下几种方式解决:

      1. 使用CSS的left属性:你可以在CSS中设置图片的left属性,使其向左移动。例如:

      css img { position: relative; left: 10px; }

      这样,图片就会在左上角对齐,同时图片的宽度也会随着页面的宽度自动调整。

      1. 使用JavaScript:你也可以使用JavaScript来控制图片的滚动。例如:

      javascript window.onscroll = function() { if (window.scrollY >= 100) { img.style.left = '10px'; } }

      这样,每当用户滚动页面时,图片就会向左移动10像素。

      1. 使用CSS的transform属性:你也可以使用CSS的transform属性来实现图片的滚动。例如:

      css img { position: absolute; left: 0; transform: translateX(-10px); }

      这样,图片就会在页面的左上角对齐,同时图片的宽度也会随着页面的宽度自动调整。

      1. 使用CSS的transform-origin属性:你还可以使用CSS的transform-origin属性来控制图片的滚动位置。例如:

      css img { position: absolute; left: 0; transform-origin: left top; }

      这样,图片就会在页面的左上角对齐,同时图片的宽度也会随着页面的宽度自动调整。

      以上就是图片向左滚动定位的问题的几种解决方式。你可以根据自己的需求选择合适的方法。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部