样式在页面加载时未正确加载。

huangapple go评论76阅读模式
英文:

style is not loading properly when page loads

问题

I'm using keen-slider@6.8.5 in a next@13.4.7 project. When my page loads in the initial state of loading before JavaScript finishes loading, I get this result image before loading properly, and when it finishes loading, I get this result, which is fine image after loading.

I don't want users to see the result of the slider like the first image. I have made a component for the slider, and this is my code. Could anyone tell me what to do?

I tried changing the flex style, but it gets stuck on one image.

This is the code of what I've done:

"use client";
import Image from "next/image";
import "./styles.css";
import "keen-slider/keen-slider.min.css";
import KeenSlider from "keen-slider";
import { useKeenSlider } from "keen-slider/react";
import React, { useRef, useState } from "react";

export default function Home() {
  const [currentSlide, setCurrentSlide] = useState(0);
  const [loaded, setLoaded] = useState(false);
  const [sliderRef, instanceRef] = useKeenSlider<HTMLDivElement>({
    initial: 0,
    slideChanged(slider) {
      setCurrentSlide(slider.track.details.rel);
    },
    created() {
      setLoaded(true);
    },
  });

  return (
    <>
      <div className="navigation-wrapper">
        <div ref={sliderRef} className="keen-slider">
          <div className="keen-slider__slide number-slide1">1</div>
          <div className="keen-slider__slide number-slide2">2</div>
          <div className="keen-slider__slide number-slide3">3</div>
          <div className="keen-slider__slide number-slide4">4</div>
          <div className="keen-slider__slide number-slide5">5</div>
          <div className="keen-slider__slide number-slide6">6</div>
        </div>
        {loaded && instanceRef.current && (
          <>
            <Arrow
              left
              onClick={(e: any) =>
                e.stopPropagation() || instanceRef.current?.prev()
              }
              disabled={currentSlide === 0}
            />
            <Arrow
              onClick={(e: any) =>
                e.stopPropagation() || instanceRef.current?.next()
              }
              disabled={
                currentSlide ===
                instanceRef.current.track.details.slides.length - 1
              }
            />
          </>
        )}
      </div>
      {loaded && instanceRef.current && (
        <div className="dots">
          {[
            ...Array(instanceRef.current.track.details.slides.length).keys(),
          ].map((idx) => {
            return (
              <button
                key={idx}
                onClick={() => {
                  instanceRef.current?.moveToIdx(idx);
                }}
                className={"dot" + (currentSlide === idx ? " active" : "")}
              ></button>
            );
          })}
        </div>
      )}
    </>
  );
}

function Arrow(props: {
  disabled: boolean;
  left?: boolean;
  onClick: (e: any) => void;
}) {
  const disabeld = props.disabled ? " arrow--disabled" : "";
  return (
    <svg
      onClick={props.onClick}
      className={`arrow ${
        props.left ? "arrow--left" : "arrow--right"
      } ${disabeld}`}
      xmlns="http://www.w3.org/2000/svg"
      viewBox="0 0 24 24"
    >
      {props.left && (
        <path d="M16.67 0l2.83 2.829-9.339 9.175 9.339 9.167-2.83 2.829-12.17-11.996z" />
      )}
      {!props.left && (
        <path d="M5 3l3.057-3 11.943 12-11.943 12-3.057-3 9-9z" />
      )}
    </svg>
  );
}
[class^="number-slide"],
[class*=" number-slide"] {
  background: grey;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 50px;
  color: #fff;
  font-weight: 500;
  height: 300px;
  max-height: 100vh;
}

.number-slide1 {
  background: rgb(64, 175, 255);
  background: linear-gradient(
    128deg,
    rgba(64, 175, 255, 1) 0%,
    rgba(63, 97, 255, 1) 100%
  );
}

/* Define styles for other number-slide classes as needed */

.navigation-wrapper {
  position: relative;
}

/* Define styles for dots, dot, arrows, and arrow styles as needed */

This code snippet appears to be a React component for a slider using keen-slider in a Next.js project. If you have specific questions or need further assistance with this code, please let me know.

英文:

I'm using keen-slider@6.8.5 in a next@13.4.7 project. When my page loads in initial state of loading before javascript load finishs, I get this result image before loading properly and when it's load finishes, I get this result which is fine imae after loading.
I don't want users to see result of slider like first image. I have made a component for slider and this is my code. could anyone tell me what to do?

I tried changing flex style but it stucks on one image

this is the code of what I've done:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

&quot;use client&quot;;
import Image from &quot;next/image&quot;;
import  &quot;./styles.css&quot;;
import &quot;keen-slider/keen-slider.min.css&quot;;
import KeenSlider from &quot;keen-slider&quot;;
import { useKeenSlider } from &quot;keen-slider/react&quot;;
import React, { useRef, useState } from &quot;react&quot;;
export default function Home() {
const [currentSlide, setCurrentSlide] = useState(0);
const [loaded, setLoaded] = useState(false);
const [sliderRef, instanceRef] = useKeenSlider&lt;HTMLDivElement&gt;({
initial: 0,
slideChanged(slider) {
setCurrentSlide(slider.track.details.rel);
},
created() {
setLoaded(true);
},
});
return (
&lt;&gt;
&lt;div className=&quot;navigation-wrapper&quot;&gt;
&lt;div ref={sliderRef} className=&quot;keen-slider&quot;&gt;
&lt;div className=&quot;keen-slider__slide number-slide1&quot;&gt;1&lt;/div&gt;
&lt;div className=&quot;keen-slider__slide number-slide2&quot;&gt;2&lt;/div&gt;
&lt;div className=&quot;keen-slider__slide number-slide3&quot;&gt;3&lt;/div&gt;
&lt;div className=&quot;keen-slider__slide number-slide4&quot;&gt;4&lt;/div&gt;
&lt;div className=&quot;keen-slider__slide number-slide5&quot;&gt;5&lt;/div&gt;
&lt;div className=&quot;keen-slider__slide number-slide6&quot;&gt;6&lt;/div&gt;
&lt;/div&gt;
{loaded &amp;&amp; instanceRef.current &amp;&amp; (
&lt;&gt;
&lt;Arrow
left
onClick={(e: any) =&gt;
e.stopPropagation() || instanceRef.current?.prev()
}
disabled={currentSlide === 0}
/&gt;
&lt;Arrow
onClick={(e: any) =&gt;
e.stopPropagation() || instanceRef.current?.next()
}
disabled={
currentSlide ===
instanceRef.current.track.details.slides.length - 1
}
/&gt;
&lt;/&gt;
)}
&lt;/div&gt;
{loaded &amp;&amp; instanceRef.current &amp;&amp; (
&lt;div className=&quot;dots&quot;&gt;
{[
...Array(instanceRef.current.track.details.slides.length).keys(),
].map((idx) =&gt; {
return (
&lt;button
key={idx}
onClick={() =&gt; {
instanceRef.current?.moveToIdx(idx);
}}
className={&quot;dot&quot; + (currentSlide === idx ? &quot; active&quot; : &quot;&quot;)}
&gt;&lt;/button&gt;
);
})}
&lt;/div&gt;
)}
&lt;/&gt;
);
}
function Arrow(props: {
disabled: boolean;
left?: boolean;
onClick: (e: any) =&gt; void;
}) {
const disabeld = props.disabled ? &quot; arrow--disabled&quot; : &quot;&quot;;
return (
&lt;svg
onClick={props.onClick}
className={`arrow ${
props.left ? &quot;arrow--left&quot; : &quot;arrow--right&quot;
} ${disabeld}`}
xmlns=&quot;http://www.w3.org/2000/svg&quot;
viewBox=&quot;0 0 24 24&quot;
&gt;
{props.left &amp;&amp; (
&lt;path d=&quot;M16.67 0l2.83 2.829-9.339 9.175 9.339 9.167-2.83 2.829-12.17-11.996z&quot; /&gt;
)}
{!props.left &amp;&amp; (
&lt;path d=&quot;M5 3l3.057-3 11.943 12-11.943 12-3.057-3 9-9z&quot; /&gt;
)}
&lt;/svg&gt;
);
}

<!-- language: lang-css -->

[class^=&quot;number-slide&quot;],
[class*=&quot; number-slide&quot;] {
background: grey;
display: flex;
align-items: center;
justify-content: center;
font-size: 50px;
color: #fff;
font-weight: 500;
height: 300px;
max-height: 100vh;
}
.number-slide1 {
background: rgb(64, 175, 255);
background: linear-gradient(
128deg,
rgba(64, 175, 255, 1) 0%,
rgba(63, 97, 255, 1) 100%
);
}
.number-slide2 {
background: rgb(255, 75, 64);
background: linear-gradient(
128deg,
rgba(255, 154, 63, 1) 0%,
rgba(255, 75, 64, 1) 100%
);
}
.number-slide3 {
background: rgb(182, 255, 64);
background: linear-gradient(
128deg,
rgba(182, 255, 64, 1) 0%,
rgba(63, 255, 71, 1) 100%
);
background: linear-gradient(
128deg,
rgba(189, 255, 83, 1) 0%,
rgba(43, 250, 82, 1) 100%
);
}
.number-slide4 {
background: rgb(64, 255, 242);
background: linear-gradient(
128deg,
rgba(64, 255, 242, 1) 0%,
rgba(63, 188, 255, 1) 100%
);
}
.number-slide5 {
background: rgb(255, 64, 156);
background: linear-gradient(
128deg,
rgba(255, 64, 156, 1) 0%,
rgba(255, 63, 63, 1) 100%
);
}
.number-slide6 {
background: rgb(64, 76, 255);
background: linear-gradient(
128deg,
rgba(64, 76, 255, 1) 0%,
rgba(174, 63, 255, 1) 100%
);
}
.navigation-wrapper {
position: relative;
}
.dots {
display: flex;
padding: 10px 0;
justify-content: center;
}
.dot {
border: none;
width: 10px;
height: 10px;
background: #c5c5c5;
border-radius: 50%;
margin: 0 5px;
padding: 5px;
cursor: pointer;
}
.dot:focus {
outline: none;
}
.dot.active {
background: #000;
}
.arrow {
width: 30px;
height: 30px;
position: absolute;
top: 50%;
transform: translateY(-50%);
-webkit-transform: translateY(-50%);
fill: #fff;
cursor: pointer;
}
.arrow--left {
left: 5px;
}
.arrow--right {
left: auto;
right: 5px;
}
.arrow--disabled {
fill: rgba(255, 255, 255, 0.5);
}

<!-- end snippet -->

答案1

得分: 1

编辑:
示例已编辑,以显示第一张幻灯片作为占位图,直到滑块库成功加载。编辑引入了以下代码来替换之前的加载占位符:

<div className={`number-slide1 ${loaded ? 'slide-cover' : ''}`}>1</div>

如果您只需要在初始化Keen Slider库之前隐藏图像,您可以使用CSS:

// 导入语句

export default () => {
...

  return (
    <>
      {/* 自定义加载组件;一旦滑块库加载完毕就移除 */}
      <div className={`number-slide1 ${loaded ? 'slide-cover' : ''}`}>1</div>
      <div ref={sliderRef} className={`keen-slider ${loaded ? "" : "slide-hidden"}`}>
        <div className="keen-slider__slide number-slide1">1</div>
        <div className="keen-slider__slide number-slide2">2</div>
      </div>
    </>
  );
}

styles.css

.slide-cover {
    display: none;
}
.slide-hidden {
    visibility: hidden;
}
英文:

Edit:
The example has been edited to display the first slide as a placeholder image until the slider library has been successfully loaded. The edit introduces replaces the previous loading placeholder with the line:

&lt;div className={`number-slide1 ${loaded ? &#39;slide-cover&#39; : &#39;&#39;}`}&gt;1&lt;/div&gt;

If you only need to hide the images prior to the initialization of the Keen Slider library, you can use CSS:

// imports

export default () =&gt; {
...

  return (
    &lt;&gt;
      // custom loading component; removed once the slider library has been loaded
      &lt;div className={`number-slide1 ${loaded ? &#39;slide-cover&#39; : &#39;&#39;}`}&gt;1&lt;/div&gt;
      &lt;div ref={sliderRef} className={`keen-slider ${loaded ? &quot;&quot; : &quot;slide-hidden&quot;}`}&gt;
        &lt;div className=&quot;keen-slider__slide number-slide1&quot;&gt;1&lt;/div&gt;
        &lt;div className=&quot;keen-slider__slide number-slide2&quot;&gt;2&lt;/div&gt;
      &lt;/div&gt;
    &lt;/&gt;
  );
}

styles.css

.slide-cover {
    display: none;
}
.slide-hidden {
    visibility: hidden;
}

huangapple
  • 本文由 发表于 2023年7月18日 15:16:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76710324.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定