Carousel会在单击箭头按钮时“跳转”到一个位置。

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

Carousel will "jump" to a position when the arrow button is clicked

问题

我发现了一个赞赏的轮播效果,并想在我的项目中使用它。但是有一个问题,一旦单击箭头按钮,它会强制整个页面跳转到特定位置并播放下一张幻灯片(但在打开检查器时不会发生这种情况)。这是唯一奇怪的部分。有没有专家可以帮助我看看是否可以修复?

<h1>纯CSS轮播</h1>
<p>此轮播仅使用HTML和CSS创建。</p>
<section class="carousel" aria-label="Gallery">
  <ol class="carousel__viewport">
    <li id="carousel__slide1" tabindex="0" class="carousel__slide">
      <div class="carousel__snapper">
        <a href="#carousel__slide4" class="carousel__prev">转到上一张幻灯片</a>
        <a href="#carousel__slide2" class="carousel__next">转到下一张幻灯片</a>
      </div>
    </li>
    <li id="carousel__slide2" tabindex="0" class="carousel__slide">
      <div class="carousel__snapper"></div>
      <a href="#carousel__slide1" class="carousel__prev">转到上一张幻灯片</a>
      <a href="#carousel__slide3" class="carousel__next">转到下一张幻灯片</a>
    </li>
    <li id="carousel__slide3" tabindex="0" class="carousel__slide">
      <div class="carousel__snapper"></div>
      <a href="#carousel__slide2" class="carousel__prev">转到上一张幻灯片</a>
      <a href="#carousel__slide4" class="carousel__next">转到下一张幻灯片</a>
    </li>
    <li id="carousel__slide4" tabindex="0" class="carousel__slide">
      <div class="carousel__snapper"></div>
      <a href="#carousel__slide3" class="carousel__prev">转到上一张幻灯片</a>
      <a href="#carousel__slide1" class="carousel__next">转到第一张幻灯片</a>
    </li>
  </ol>
  <aside class="carousel__navigation">
    <ol class="carousel__navigation-list">
      <li class="carousel__navigation-item">
        <a href="#carousel__slide1" class="carousel__navigation-button">转到幻灯片1</a>
      </li>
      <li class="carousel__navigation-item">
        <a href="#carousel__slide2" class="carousel__navigation-button">转到幻灯片2</a>
      </li>
      <li class="carousel__navigation-item">
        <a href="#carousel__slide3" class="carousel__navigation-button">转到幻灯片3</a>
      </li>
      <li class="carousel__navigation-item">
        <a href="#carousel__slide4" class="carousel__navigation-button">转到幻灯片4</a>
      </li>
    </ol>
  </aside>
</section>

感谢您抽出时间阅读我的问题,谢谢。

我尝试更改CSS以修复问题,但是找不到正确的方法。".carousel"是控制包含整个轮播的外部框的属性,因为我只需要它占据屏幕的一部分,所以我将其padding-top更改为25%。

.carousel {
  position: relative;
  padding-top: 25%; /* 修改为25% */
  filter: drop-shadow(0 0 10px #0003);
  perspective: 90px;
}

由于".carousel_viewport"是上一个元素的内部部分,因此我还尝试更改它的"top right bottom left"这四个属性,但没有任何变化。

.carousel__viewport {
  position: absolute;
  top: 20px; /* 尝试更改此值 */
  right: 0; /* 尝试更改此值 */
  bottom: 20px; /* 尝试更改此值 */
  left: 0; /* 尝试更改此值 */
  display: flex;
  overflow-x: scroll;
  counter-reset: item;
  scroll-behavior: smooth;
  scroll-snap-type: x mandatory;
}

我还尝试了下面的属性,但仍然失败。

.carousel::before,
.carousel::after,
.carousel__prev,
.carousel__next {
  position: absolute;
  top: 50px; /* 尝试更改此值 */
  margin-top: 17%; /* 尝试更改此值 */
}

希望这能帮助您解决问题。

英文:

I found a carousel that i love its effect and want to use in my project.
However there is a problem that once you click on the arrow button, it will force the whole page to goto a certain position and play the next slide-show.(but this is not happened when opened inspector)
This is the only part that is weird. Could any expert help me to see whether can fix?

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

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

@keyframes tonext {
75% {
left: 0;
}
95% {
left: 100%;
}
98% {
left: 100%;
}
99% {
left: 0;
}
}
@keyframes tostart {
75% {
left: 0;
}
95% {
left: -300%;
}
98% {
left: -300%;
}
99% {
left: 0;
}
}
@keyframes snap {
96% {
scroll-snap-align: center;
}
97% {
scroll-snap-align: none;
}
99% {
scroll-snap-align: none;
}
100% {
scroll-snap-align: center;
}
}
body {
max-width: 37.5rem;
margin: 0 auto;
padding: 0 1.25rem;
font-family: &#39;Lato&#39;, sans-serif;
}
* {
box-sizing: border-box;
scrollbar-color: transparent transparent;
/* thumb and track color */
scrollbar-width: 0px;
}
*::-webkit-scrollbar {
width: 0;
}
*::-webkit-scrollbar-track {
background: transparent;
}
*::-webkit-scrollbar-thumb {
background: transparent;
border: none;
}
* {
-ms-overflow-style: none;
}
ol,
li {
list-style: none;
margin: 0;
padding: 0;
}
.carousel {
position: relative;
padding-top: 70%;
filter: drop-shadow(0 0 10px #0003);
perspective: 90px;
}
.carousel__viewport {
position: absolute;
top: 20px;
right: 0;
bottom: 20px;
left: 0;
display: flex;
overflow-x: scroll;
counter-reset: item;
scroll-behavior: smooth;
scroll-snap-type: x mandatory;
}
.carousel__slide {
position: relative;
flex: 0 0 100%;
width: 100%;
background-color: #f99;
counter-increment: item;
}
.carousel__slide:nth-child(even) {
background-color: #99f;
}
.carousel__slide:before {
content: counter(item);
position: absolute;
top: 80%;
left: 50%;
transform: translate3d(-50%, -40%, 70px);
color: #fff;
font-size: 2em;
}
.carousel__snapper {
position: absolute;
top: 0px;
left: 0;
width: 100%;
height: 100%;
scroll-snap-align: center;
}
@media (hover: hover) {
.carousel__snapper {
animation-name: tonext, snap;
animation-timing-function: ease;
animation-duration: 4s;
animation-iteration-count: infinite;
}
.carousel__slide:last-child .carousel__snapper {
animation-name: tostart, snap;
}
}
@media (prefers-reduced-motion: reduce) {
.carousel__snapper {
animation-name: none;
}
}
.carousel:hover .carousel__snapper,
.carousel:focus-within .carousel__snapper {
animation-name: none;
}
.carousel__navigation {
position: absolute;
right: 0;
bottom: 0;
left: 0;
text-align: center;
}
.carousel__navigation-list,
.carousel__navigation-item {
display: inline-block;
}
.carousel__navigation-button {
display: inline-block;
width: 1.5rem;
height: 1.5rem;
background-color: #333;
background-clip: content-box;
border: 0.25rem solid transparent;
border-radius: 50%;
font-size: 0;
transition: transform 0.1s;
}
.carousel::before,
.carousel::after,
.carousel__prev,
.carousel__next {
position: absolute;
top: 50px;
margin-top: 17%;
width: 4rem;
height: 4rem;
transform: translateY(-50%);
border-radius: 50%;
font-size: 0;
outline: 0;
}
.carousel::before,
.carousel__prev {
left: -1rem;
}
.carousel::after,
.carousel__next {
right: -1rem;
}
.carousel::before,
.carousel::after {
content: &#39;&#39;;
z-index: 1;
background-color: #333;
background-size: 1.5rem 1.5rem;
background-repeat: no-repeat;
background-position: center center;
color: #fff;
font-size: 2.5rem;
line-height: 10rem;
text-align: center;
pointer-events: none;
}
.carousel::before {
background-image: url(&quot;data:image/svg+xml,%3Csvg viewBox=&#39;0 0 100 100&#39; xmlns=&#39;http://www.w3.org/2000/svg&#39;%3E%3Cpolygon points=&#39;0,50 80,100 80,0&#39; fill=&#39;%23fff&#39;/%3E%3C/svg%3E&quot;);
}
.carousel::after {
background-image: url(&quot;data:image/svg+xml,%3Csvg viewBox=&#39;0 0 100 100&#39; xmlns=&#39;http://www.w3.org/2000/svg&#39;%3E%3Cpolygon points=&#39;100,50 20,100 20,0&#39; fill=&#39;%23fff&#39;/%3E%3C/svg%3E&quot;);
}

<!-- language: lang-html -->

&lt;h1&gt;CSS-only Carousel&lt;/h1&gt;
&lt;p&gt;This carousel is created with HTML and CSS only.&lt;/p&gt;
&lt;section class=&quot;carousel&quot; aria-label=&quot;Gallery&quot;&gt;
&lt;ol class=&quot;carousel__viewport&quot;&gt;
&lt;li id=&quot;carousel__slide1&quot; tabindex=&quot;0&quot; class=&quot;carousel__slide&quot;&gt;
&lt;div class=&quot;carousel__snapper&quot;&gt;
&lt;a href=&quot;#carousel__slide4&quot; class=&quot;carousel__prev&quot;&gt;Go to last slide&lt;/a&gt;
&lt;a href=&quot;#carousel__slide2&quot; class=&quot;carousel__next&quot;&gt;Go to next slide&lt;/a&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li id=&quot;carousel__slide2&quot; tabindex=&quot;0&quot; class=&quot;carousel__slide&quot;&gt;
&lt;div class=&quot;carousel__snapper&quot;&gt;&lt;/div&gt;
&lt;a href=&quot;#carousel__slide1&quot; class=&quot;carousel__prev&quot;&gt;Go to previous slide&lt;/a&gt;
&lt;a href=&quot;#carousel__slide3&quot; class=&quot;carousel__next&quot;&gt;Go to next slide&lt;/a&gt;
&lt;/li&gt;
&lt;li id=&quot;carousel__slide3&quot; tabindex=&quot;0&quot; class=&quot;carousel__slide&quot;&gt;
&lt;div class=&quot;carousel__snapper&quot;&gt;&lt;/div&gt;
&lt;a href=&quot;#carousel__slide2&quot; class=&quot;carousel__prev&quot;&gt;Go to previous slide&lt;/a&gt;
&lt;a href=&quot;#carousel__slide4&quot; class=&quot;carousel__next&quot;&gt;Go to next slide&lt;/a&gt;
&lt;/li&gt;
&lt;li id=&quot;carousel__slide4&quot; tabindex=&quot;0&quot; class=&quot;carousel__slide&quot;&gt;
&lt;div class=&quot;carousel__snapper&quot;&gt;&lt;/div&gt;
&lt;a href=&quot;#carousel__slide3&quot; class=&quot;carousel__prev&quot;&gt;Go to previous slide&lt;/a&gt;
&lt;a href=&quot;#carousel__slide1&quot; class=&quot;carousel__next&quot;&gt;Go to first slide&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;aside class=&quot;carousel__navigation&quot;&gt;
&lt;ol class=&quot;carousel__navigation-list&quot;&gt;
&lt;li class=&quot;carousel__navigation-item&quot;&gt;
&lt;a href=&quot;#carousel__slide1&quot; class=&quot;carousel__navigation-button&quot;&gt;Go to slide 1&lt;/a&gt;
&lt;/li&gt;
&lt;li class=&quot;carousel__navigation-item&quot;&gt;
&lt;a href=&quot;#carousel__slide2&quot; class=&quot;carousel__navigation-button&quot;&gt;Go to slide 2&lt;/a&gt;
&lt;/li&gt;
&lt;li class=&quot;carousel__navigation-item&quot;&gt;
&lt;a href=&quot;#carousel__slide3&quot; class=&quot;carousel__navigation-button&quot;&gt;Go to slide 3&lt;/a&gt;
&lt;/li&gt;
&lt;li class=&quot;carousel__navigation-item&quot;&gt;
&lt;a href=&quot;#carousel__slide4&quot; class=&quot;carousel__navigation-button&quot;&gt;Go to slide 4&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/aside&gt;
&lt;/section&gt;

<!-- end snippet -->

It is appreciated that you use time to read my dump question, thanks

I tried to change the CSS in order to fix however cannot find a right way.
".carousel" is the property that control the outer-box that contains the whole carousel, as i only need it to obtain part of the screen, i changed its padding-top to 25%

As
'.carousel_viewport' is the inner part of previous, so i also changed its "top right bottom left" these four, but nothing changed

Also tried below properties but still fail.

.carousel::before,
.carousel::after,
.carousel__prev,
.carousel__next {
position: absolute;
top: 50px;
margin-top: 17%;
}

答案1

得分: 0

似乎从我所知道的来看,你不能禁用这种行为,这是使用这个特定的仅CSS滑块的缺点。

这是因为滑块控件使用锚点,例如按钮是带有形式为#slide_to_reach的地址的链接(&lt;a&gt;标签),HTML中锚点的原始目的是到达(滚动到)文档的特定部分。

因此,这是所有浏览器内置的功能,通过点击按钮,你将滚动到锚点。要找到一种不使用这个特定滑块滚动的方法意味着滑块将不再工作,抱歉。

英文:

It seems you cannot disable this behaviour from what I know, this is the downside of using this specific CSS-only slider.

This is because the slider controls are using anchors, eg the buttons are links (&lt;a&gt; tags) with adresses of the form #slide_to_reach. The original goal of anchors in HTML was to reach (scroll to) a specific part of the document.

So this is a built-in in all browsers, by clicking on the buttons you will scroll to the anchor. To find a way not to scroll with this specific slider would mean the slider will no longer work, sorry.

huangapple
  • 本文由 发表于 2023年6月5日 21:56:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76407166.html
匿名

发表评论

匿名网友

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

确定