元素的offsetWidth似乎是随机变化

问题描述 投票:0回答:1

我注意到offsetWidththis.ratingSliderInput = document.querySelector(".js-rating-slider-input")随机变化。

它从实际宽度变为现在,然后再切换到129(不知道该值从哪里来。)>

这会负面影响设置this.ratingSliderThumb的位置。

我现在能够解决此问题的方法是使用setTimeout函数。像这样:

setTimeout(() => {
  this.setPositionThumb();
  this.setThumbStyle();
}, 1);

但是,我仍然有兴趣找出为什么当我没有offsetWidthsetTimeout随机更改为129的原因,因为setTimeout修复导致拇指位置首先出现的难看故障变为0,然后变为它拥有的任何值(评分得分)。

JavaScript:

class RatingSlider {
  constructor() {
    this.ratingSliderForm = document.querySelector(".js-rating-slider-form");
    this.ratingSliderInput = document.querySelector(".js-rating-slider-input");
    this.ratingSliderThumb = document.querySelector(".js-rating-slider-thumb");
    this.ratingSliderValue = document.querySelector(".js-rating-slider-value");
    this.ratingSliderIcon = document.querySelector(".js-rating-slider-icon");
    this.isPressed = false;
    this.setEvents();
    this.bind();
  }
  setEvents() {
    this.moveEvent;
    this.startEvent;
    this.endEvent;
    if ("ontouchstart" in document.documentElement) {
      this.moveEvent = "touchmove";
      this.startEvent = "touchstart";
      this.endEvent = "touchend";
    } else {
      this.moveEvent = "mousemove";
      this.startEvent = "mousedown";
      this.endEvent = "mouseup";
    }
  }

  setThumbStyle() {
    this.ratingSliderIcon.style.transform = `scale(${1 +
      this.ratingSliderInput.value / 150})`;
    this.ratingSliderValue.innerText = `${this.ratingSliderInput.value}°`;
  }

  setPositionThumb() {
    this.ratingSliderThumb.style.left = `${(this.ratingSliderInput.offsetWidth /
      100) *
      this.ratingSliderInput.value -
      10}px`;
  }

  handleOffsetOnChange(event) {
    if ("ontouchstart" in document.documentElement) {
      let touch = event.touches[0] || event.changedTouches[0];
      let target = document.elementFromPoint(touch.clientX, touch.clientY);
      event.offsetX = touch.clientX - target.getBoundingClientRect().x;
    }
    if (
      event.offsetX > 0 &&
      event.offsetX < this.ratingSliderInput.offsetWidth
    ) {
      this.ratingSliderThumb.style.left = `${event.offsetX - 10}px`;
    }
  }

  bind() {
    if (!this.ratingSliderForm) {
      return;
    }
    this.setPositionThumb();
    this.setThumbStyle();

    this.ratingSliderInput.addEventListener(
      this.startEvent,
      () => (this.isPressed = true)
    );

    this.ratingSliderInput.addEventListener(this.endEvent, () => {
      this.isPressed = false;
      this.ratingSliderForm.submit();
    });

    this.ratingSliderInput.addEventListener(this.moveEvent, (event) => {
      if (!this.isPressed) {
        return;
      }
      this.handleOffsetOnChange(event);
      this.setThumbStyle();
    });
  }
}

export default RatingSlider;

CSS:

.rating-slider__inner-container {
  position: relative;
  padding-top: 100px;
}

.rating-slider__input {
  -webkit-appearance: none;
  width: 100%;
  height: 5px;
  background: $form-gray;
  &:focus {
    outline: none;
  }
}

.rating-slider__input::-webkit-slider-thumb {
  -webkit-appearance: none;
  background: transparent;
  border-color: transparent;
  height: 20px;
  width: 20px;
  margin-top: -8px;
  cursor: pointer;
}

.rating-slider__input::-moz-range-thumb {
  background: transparent;
  border-color: transparent;
  height: 20px;
  width: 20px;
  cursor: pointer;
}

.rating-slider__input::-moz-focus-outer {
  border: 0;
}

.rating-slider__thumb {
  display: flex;
  flex-direction: column;
  align-items: center;
  position: absolute;
  top: 50px;
  left: -10px;
  font-size: $text-3xl;
  pointer-events: none;
}

.rating-slider__value {
  color: $brand-primary;
  font-size: $text-lg;
}

.rating-slider__range-labels-container {
  display: flex;
  justify-content: space-between;
}

这里是项目进行中:https://wagon-city-guides.herokuapp.com/spots/32

和GitHub上的代码:

JS:

https://github.com/mirhamasala/lw_city_guide/blob/master/app/javascript/components/rating_slider.js

CSS:

https://github.com/mirhamasala/lw_city_guide/blob/master/app/assets/stylesheets/components/_rating_slider.scss

HTML:

https://github.com/mirhamasala/lw_city_guide/blob/master/app/views/spots/_spot_rating.html.erb

[我注意到this.ratingSliderInput = document.querySelector(“。js-rating-slider-input”)的offsetWidth随机变化。它从实际宽度变为现在,然后切换到129(no ...

javascript dom
1个回答
0
投票

[我意识到您的错误仅出现在页面的重新加载中,而不是来自另一个被怀疑为涡轮链接的罪魁祸首(经常...)

© www.soinside.com 2019 - 2024. All rights reserved.