简体   繁体   English

使用 Vue 添加左右滑动

[英]adding left and right swipe with Vue

helping a client with a website with barely any JavaScript knowledge but have come this far and now looking for a simple and quick code addition that can add left/right swipe functions to the Hero and Hero-BG tags帮助客户的网站几乎没有任何 JavaScript 知识,但已经走到了这一步,现在正在寻找一个简单快捷的代码添加,可以向 Hero 和 Hero-BG 标签添加左/右滑动功能

this is the example: http://nufaith.ca/justinatkins这是一个例子: http://nufaith.ca/justinatkins

so whenever the user swipes left or right both the main hero image and the blurred hero image above it will both scroll to the next image much the same as clicking the chevrons or pressing arrow keys on a keyboard因此,每当用户向左或向右滑动主主图像和上方的模糊主图像时,它都会滚动到下一个图像,就像单击 V 形或按键盘上的箭头键一样

Vue.component("hero-bg", {
  template: `
    <div class="hero-bg">
      <div class="hero">
        <img id="pushed" :src="selected" />
      </div>
    </div>
    `,
  props: ["selected"]
});

Vue.component("hero", {
  template: `
    <div>
      <topbar></topbar>
      <topbarmob></topbarmob>
      <hero-bg :selected="selectedItem.img"></hero-bg>
      <div class="hero-container" v-if="!gridEnabled">
        <div class="hero">
          <img :src="selectedItem.img" v-if="thing" alt=""/>
        </div>

        <div class="hero-desc">
          <button class="control left" @click="previous">
            <i class="zmdi zmdi-chevron-left"></i>
          </button>
          <span class="hero-desc-title" v-html="title"></span>
          <button class="control right" @click="next">
            <i class="zmdi zmdi-chevron-right"></i>
          </button>
          <br/>
          <button class="view-all-button" @click="enableGrid">OVERVIEW</button>
        </div>
      </div>
    </div>
    `,
  data() {
    return {
      gridEnabled: false,
      selected: 0,
      thing: true
    };
  },
  computed: {
    selectedItem() {
      return info[this.selected];
    },
    title() {
      const comma = this.selectedItem.title.indexOf(",");
      const len = this.selectedItem.title.length;
      const strBeginning = this.selectedItem.title.substring(comma, 0);
      const strEnd = this.selectedItem.title.substring(comma, len);
      if (this.selectedItem.title.includes(",")) {
        return `<span>${strBeginning}<span class="font-regular font-muted">${strEnd}</span></span>`;
      }
      return this.selectedItem.title;
    },
    maxImages() {
      return info.length - 1;
    }
  },
  created() {
    window.addEventListener("keydown", e => {
      if (e.keyCode === 37) {
        this.previous();
        return;
      }

      if (e.keyCode === 39) {
        this.next();
        return;
      }
    });
    Event.$on("updateImg", index => {
      this.selected = index;
      this.gridEnabled = !this.gridEnabled;
    });
  },
  methods: {
    next() {
      this.selected === this.maxImages
        ? (this.selected = 0)
        : (this.selected += 1);
    },
    previous() {
      this.selected === 0
        ? (this.selected = this.maxImages)
        : (this.selected -= 1);
    },
    enableGrid() {
      this.gridEnabled = !this.gridEnabled;
      window.scroll(0, 0);
      Event.$emit("enableGrid");
    }
  }
});

You could try to act on the touchstart and touchend javascript events and use them to determine which way the user swipes.您可以尝试对 touchstart 和 touchend javascript 事件采取行动,并使用它们来确定用户滑动的方式。

So...所以...

<div @touchstart="touchStartMethod" @touchEndMethod="touchEndMethod"> ...</div>

... ...

    methods: {
           touchStartMethod (touchEvent) {
              console.log(touchEvent)
              // For example under changedTouches you'll see that clientX changes
              // So if it grows from start to finish you know it is swipe right
           },
           touchEndMethod (touchEvent) {...}        
     }

Edit编辑

First, we want to act on the touchstart event where we want to swipe( I assume it is the div with the class hero-container)首先,我们想要对想要滑动的 touchstart 事件采取行动(我假设它是带有 class hero-container 的 div)

<div class="hero-container" v-if="!gridEnabled" @touchstart="touchStart">...

Then, we add the following methods to "methods":然后,我们将以下方法添加到“方法”中:

      touchStart (touchEvent) {
        if (touchEvent.changedTouches.length !== 1) { // We only care if one finger is used
          return;
        }
        const posXStart = touchEvent.changedTouches[0].clientX;
        addEventListener('touchend', (touchEvent) => this.touchEnd(touchEvent, posXStart), {once: true});
      },
      touchEnd (touchEvent, posXStart) {
        if (touchEvent.changedTouches.length !== 1) { // We only care if one finger is used
          return;
        }
        const posXEnd = touchEvent.changedTouches[0].clientX;
        if (posXStart < posXEnd) {
          this.previous(); // swipe right
        } else if (posXStart > posXEnd) {
          this.next(); // swipe left
        }
      }

I changed the touch end part to a event listener that is used once for the touchend event, just in case the finger is outside the container when it lets go of the screen.我将触摸结束部分更改为一个事件侦听器,该侦听器用于一次触摸结束事件,以防万一手指在容器之外时它让 go 进入屏幕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM