melkaya94
@melkaya94

Как при загрузке страницы менять стили?

При загрузке страницы в мобилке стили должны меняться, аналогично и при ресайзе. На ресайз код работает, но если загружаем сразу с мобильного, то нужные стили не применяются

static propTypes = {
		common: T.object.isRequired
	};

	constructor(...args) {
		super(...args);
		this.getLayout = ::this.getLayout;
		this.rafResize = ::this.rafResize;
		this.checkLayoutUpdates = ::this.checkLayoutUpdates;

		this.state = {
			isMobile: this.props.common.isMobile,
			layout: this.getLayout()
		};
	}

	componentDidMount() {
		this.checkLayoutUpdates();
		this.eventOptions = detectPassiveEvents.hasSupport === true ? {capture: false, passive: true} : false;
		window.addEventListener('resize', this.rafResize, this.eventOptions);
	}

	componentWillUnmount() {
		window.removeEventListener('resize', this.rafResize, this.eventOptions);
	}
	getLayout() {
		if (!__BROWSER__) {  // eslint-disable-line no-undef
			const {common} = this.props;
			return common.isMobile ? 'md' : common.isTablet ? 'vt' : 'dd';
		}
		const WW = document.documentElement.clientWidth || window.innerWidth;

		if (WW < 720) {
			return 'md';
		}

		if (WW < 990) {
			return 'vt';
		}

		if (WW < 1250) {
			return 'ht';
		}

		return 'dd';
	}

	checkLayoutUpdates() {
		const layout = this.getLayout();
		if (this.state.layout !== layout) {
			this.setState({layout, isMobile: layout === 'md', isTablet: layout === 'vt'});
		}
	}

	rafResize() {
		requestAnimationFrame(this.checkLayoutUpdates);
	}

        render() {
		const {layout, isMobile} = this.state;
		return (
			<OnVisible>
          {isMobile ? (<div className={styles.mobile_container}></div>) :(<div className={styles.event_container></div>}
            </OnVisible>
  • Вопрос задан
  • 169 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
18 мая 2024, в 21:07
5000 руб./за проект
18 мая 2024, в 20:58
5000 руб./за проект
18 мая 2024, в 20:53
500 руб./за проект