@satico (乃木 さちこ)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

cssだけで、ループ再生で、矢印でも移動出来るカルーセルスライダーを作りたい

解決したいこと

cssだけで、ループ再生で、矢印でも移動出来るカルーセルスライダーを作りたい

cssだけで、矢印で移動できるスライダーを作りました。
矢印に加えて、ループ再生で、自動的に画像が流れるようにしたいのですが、上手くいきません。

ループ再生だけなら、translateX などでアニメーションにすると出来るのですが、矢印での移動も出来るようにすると、矢印は margin-left で移動しているからか、画像と画像の間に隙間が空いてしまったり、矢印をクリックした時にちょうどいい位置に来なかったりします。

ループ再生と、矢印での移動と、両方をcssだけで出来るようにするのは難しいでしょうか。

html

<div class="csslider infinity responsive-wrap">
    <input type="radio" name="slides" id="slides_1" checked />
    <input type="radio" name="slides" id="slides_2" />
    <input type="radio" name="slides" id="slides_3" />
    <input type="radio" name="slides" id="slides_4" />
    <input type="radio" name="slides" id="slides_5" />

    <ul>
        <li><img src="images/slide1.jpg"></li>
        <li><img src="images/slide2.jpg"></li>
        <li><img src="images/slide3.jpg"></li>
        <li><img src="images/slide4.jpg"></li>
        <li><img src="images/slide5.jpg"></li>
    </ul>

    <div class="arrows">
        <label for="slides_1"></label>
        <label for="slides_2"></label>
        <label for="slides_3"></label>
        <label for="slides_4"></label>
        <label for="slides_5"></label>
    </div>
</div>

css

.csslider{
  display: flex;
  position: relative;
}

.csslider > input { display: none;}

.csslider > input:nth-of-type(10):checked ~ ul li:first-of-type {
  margin-left: -900%;
}
.csslider > input:nth-of-type(9):checked ~ ul li:first-of-type {
  margin-left: -800%;
}
.csslider > input:nth-of-type(8):checked ~ ul li:first-of-type {
  margin-left: -700%;
}
.csslider > input:nth-of-type(7):checked ~ ul li:first-of-type {
  margin-left: -600%;
}
.csslider > input:nth-of-type(6):checked ~ ul li:first-of-type {
  margin-left: -500%;
}
.csslider > input:nth-of-type(5):checked ~ ul li:first-of-type {
  margin-left: -400%;
}
.csslider > input:nth-of-type(4):checked ~ ul li:first-of-type {
  margin-left: -300%;
}
.csslider > input:nth-of-type(3):checked ~ ul li:first-of-type {
  margin-left: -200%;
}
.csslider > input:nth-of-type(2):checked ~ ul li:first-of-type {
  margin-left: -100%;
}
.csslider > input:nth-of-type(1):checked ~ ul li:first-of-type {
  margin-left: 0;
}

.csslider > ul {
    position: relative;
    width: 100%;
    min-height: 0;
    z-index: 1;
    background-color: #3a3a3a;
    z-index: 1;
    display: flex;
    overflow-x: scroll;
    counter-reset: item;
    scroll-behavior: smooth;
    scroll-snap-type: x mandatory;  
    -ms-overflow-style: none;    /* IE, Edge 対応 */
    scrollbar-width: none;       /* Firefox 対応 */
}

.csslider > ul::-webkit-scrollbar {  /* Chrome, Safari 対応 */
    display:none;
}

.csslider > ul > li {
    position: relative;
    display: inline-block;
    flex: 0 0 100%;
    width: 100%;
    height: 100%;
    overflow: hidden;
    font-size: 15px;
    -moz-transition: all 0.5s cubic-bezier(0.4, 1.3, 0.65, 1);
    -o-transition: all 0.5s ease-out;
    -webkit-transition: all 0.5s cubic-bezier(0.4, 1.3, 0.65, 1);
    transition: all 0.5s cubic-bezier(0.4, 1.3, 0.65, 1);
    vertical-align: top;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    white-space: normal;
}

.csslider > ul > li:after {
    content: "";
    display: block;
    padding-bottom: 70%;
}

.csslider > ul > li img {
    position: absolute;
    width: 100%;
    height: 100%;
    object-fit:cover;
    top:50%;
    left: 50%;
    transform: translate(-50%,-50%);
    display: block;
}

.csslider > .arrows {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

.csslider > .arrows {
    position: absolute;
    left: 1%;
    top: 50%;
    width: 98%;
    height: 26px;
    z-index: 2;
}
.csslider > .arrows label {
    display: none;
    position: absolute;
    top: -50%;
    padding: 13px;
    box-shadow: inset 2px -2px 0 1px #fff;
    cursor: pointer;
    -moz-transition: box-shadow 0.15s, margin 0.15s;
    -o-transition: box-shadow 0.15s, margin 0.15s;
    -webkit-transition: box-shadow 0.15s, margin 0.15s;
    transition: box-shadow 0.15s, margin 0.15s;
}
.csslider > .arrows label:hover {
    box-shadow: inset 3px -3px 0 2px #0097ef;
    margin: 0 0px;
}

.csslider.infinity > input:first-of-type:checked ~ .arrows label:last-of-type,
.csslider > input:nth-of-type(1):checked ~ .arrows label:nth-of-type(0),
.csslider > input:nth-of-type(2):checked ~ .arrows label:nth-of-type(1),
.csslider > input:nth-of-type(3):checked ~ .arrows label:nth-of-type(2),
.csslider > input:nth-of-type(4):checked ~ .arrows label:nth-of-type(3),
.csslider > input:nth-of-type(5):checked ~ .arrows label:nth-of-type(4),
.csslider > input:nth-of-type(6):checked ~ .arrows label:nth-of-type(5),
.csslider > input:nth-of-type(7):checked ~ .arrows label:nth-of-type(6),
.csslider > input:nth-of-type(8):checked ~ .arrows label:nth-of-type(7),
.csslider > input:nth-of-type(9):checked ~ .arrows label:nth-of-type(8),
.csslider > input:nth-of-type(10):checked ~ .arrows label:nth-of-type(9),
.csslider > input:nth-of-type(11):checked ~ .arrows label:nth-of-type(10) {
    display: block;
    left: 0;
    -moz-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    -o-transform: rotate(45deg);
    -webkit-transform: rotate(45deg);
    transform: rotate(45deg);
}
.csslider.infinity > input:last-of-type:checked ~ .arrows label:first-of-type,
.csslider > input:nth-of-type(1):checked ~ .arrows label:nth-of-type(2),
.csslider > input:nth-of-type(2):checked ~ .arrows label:nth-of-type(3),
.csslider > input:nth-of-type(3):checked ~ .arrows label:nth-of-type(4),
.csslider > input:nth-of-type(4):checked ~ .arrows label:nth-of-type(5),
.csslider > input:nth-of-type(5):checked ~ .arrows label:nth-of-type(6),
.csslider > input:nth-of-type(6):checked ~ .arrows label:nth-of-type(7),
.csslider > input:nth-of-type(7):checked ~ .arrows label:nth-of-type(8),
.csslider > input:nth-of-type(8):checked ~ .arrows label:nth-of-type(9),
.csslider > input:nth-of-type(9):checked ~ .arrows label:nth-of-type(10),
.csslider > input:nth-of-type(10):checked ~ .arrows label:nth-of-type(11),
.csslider > input:nth-of-type(11):checked ~ .arrows label:nth-of-type(12){
    display: block;
    right: 0;
    -moz-transform: rotate(225deg);
    -ms-transform: rotate(225deg);
    -o-transform: rotate(225deg);
    -webkit-transform: rotate(225deg);
    transform: rotate(225deg);
}
0 likes

No Answers yet.

Your answer might help someone💌