使用 React 滑动 JS

Glide JS with react

按照文档进行操作并得到以下错误:类型错误:无法读取未定义的 属性 'querySelector'。我已将以下内容添加到我的 index.html

<link
      rel="stylesheet"
      href="node_modules/@glidejs/glide/dist/css/glide.core.min.css"
    /> 

出于某种原因,滑行破坏了我的整个应用程序。为了让 GlideJS 在 create-react-app 中工作,我需要执行另一个步骤吗?任何建议都会有很大帮助。谢谢! 我的代码如下:

import React from "react"
import Glide from "@glidejs/glide"

const Test = () => {
  new Glide(".glide").mount()
  return (
    <>
      {" "}
      <div className='glide'>
        <div className='glide__track' data-glide-el='track'>
          <ul className='glide__slides'>
            <li className='glide__slide slider'>1</li>
            <li className='glide__slide slider'>2</li>
            <li className='glide__slide slider'>3</li>
            <li className='glide__slide slider'>4</li>
          </ul>
        </div>
        <div className='glide__arrows' data-glide-el='controls'>
          <button
            className='glide__arrow glide__arrow--prev'
            data-glide-dir='<'
          >
            prev
          </button>
          <button
            className='glide__arrow glide__arrow--next'
            data-glide-dir='>'
          >
            next
          </button>
        </div>
      </div>
    </>
  )
}

export default Test


使用useEffect模仿componentDidMount()。并将滑块安装在 useEffect()

尝试这样的事情:

import React, { useEffect } from "react"
import Glide from "@glidejs/glide"

// Using SCSS
import "./styles.scss"

const sliderConfiguration= {
  gap: 20,
  perView: 2,
  startAt: 0,
  type: "slider"
};

const Test = () => {
  const slider = new Glide('.glide', sliderConfiguration);

  useEffect(() => {
    return () => slider.mount()
  }, [slider])

  return (
    <>
      {" "}
      <div className='glide'>
        <div className='glide__track' data-glide-el='track'>
          <ul className='glide__slides'>
            <li className='glide__slide slider'>1</li>
            <li className='glide__slide slider'>2</li>
            <li className='glide__slide slider'>3</li>
            <li className='glide__slide slider'>4</li>
          </ul>
        </div>
        <div className='glide__arrows' data-glide-el='controls'>
          <button
            className='glide__arrow glide__arrow--prev'
            data-glide-dir='<'
          >
            prev
          </button>
          <button
            className='glide__arrow glide__arrow--next'
            data-glide-dir='>'
          >
            next
          </button>
        </div>
      </div>
    </>
  )
}

export default Test

styles.scss

// Required Core Stylesheet
@import "node_modules/@glidejs/glide/src/assets/sass/glide.core";

注: 如果您的幻灯片项目使用动态数据,您应该将包含数据的变量传递给 useEffect() 依赖项。

这只是做同样事情的不同方法:

import React, { useEffect } from 'react';

import './Test.css';

const Test = () => {
  useEffect(() => {
    // eslint-disable-next-line no-unused-vars
    window.addEventListener('load', (event) => {
      // eslint-disable-next-line no-undef
      new Glide('.glide', {
        gap: 50,
        // eslint-disable-next-line no-undef
        // focusAt: 1,
        peek: 200,
        perView: 3,
        type: 'carousel',
        breakpoints: {
          1600: {
            perView: 2,
          },
          1100: {
            perView: 1,
            peek: 80,
          },
        },
      }).mount();
    });
  }, []);

  return (
    <div className="tiers" id="tiers">
      <h2 className="sectionTitle">Tiers</h2>

      <div className="glide">
        <div className="glide__track" data-glide-el="track">
          <div className="glide__slides">
            <div className="glide__slide tierBox">
              <h4>Tier One</h4>
              <span className="tierTitle">Description:</span>

              <span className="tierTitle">Cost:</span>

              <span className="tierTitle">Starting:</span>

              <span className="tierTitle">Test:</span>

              <span className="tierTitle">Test 2:</span>
            </div>

            <div className="glide__slide tierBox">
              <h4>Tier One</h4>
              <span className="tierTitle">Description:</span>

              <span className="tierTitle">Cost:</span>

              <span className="tierTitle">Starting:</span>

              <span className="tierTitle">Test:</span>

              <span className="tierTitle">Test 2:</span>
            </div>

            <div className="glide__slide tierBox">
              <h4>Tier One</h4>
              <span className="tierTitle">Description:</span>

              <span className="tierTitle">Cost:</span>

              <span className="tierTitle">Starting:</span>

              <span className="tierTitle">Test:</span>

              <span className="tierTitle">Test 2:</span>
            </div>

            <div className="glide__slide tierBox">
              <h4>Tier One</h4>
              <span className="tierTitle">Description:</span>

              <span className="tierTitle">Cost:</span>

              <span className="tierTitle">Starting:</span>

              <span className="tierTitle">Test:</span>

              <span className="tierTitle">Test 2:</span>
            </div>

            <div className="glide__slide tierBox">
              <h4>Tier One</h4>
              <span className="tierTitle">Description:</span>

              <span className="tierTitle">Cost:</span>

              <span className="tierTitle">Starting:</span>

              <span className="tierTitle">Test:</span>

              <span className="tierTitle">Test 2:</span>
            </div>
          </div>
        </div>
        <div className="glide__arrows" data-glide-el="controls">
          <button type="button" className="glide__arrow glide__arrow--left" data-glide-dir="<">prev</button>
          <button type="button" className="glide__arrow glide__arrow--right" data-glide-dir=">">next</button>
        </div>
      </div>

    </div>
  );
};
export default Test;

如果您要将 Glide.js 脚本插入到 index.html 的头部,您也不需要从“@glidejs/glide”导入 Glide。