JS + CSS Flipcards Breaking Swiper JS

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

JS + CSS Flipcards Breaking Swiper JS

问题

我目前正在尝试将简单的卡片翻转动画应用到个人网站的 SwiperJS 走马灯上。

// 代码部分不翻译
/* 代码部分不翻译 */
<!-- 代码部分不翻译 -->

在轮播中的除了最后一张卡片之外的所有卡片都能正常工作,只有一些小的格式问题。

尝试将卡片翻转效果应用到轮播中的最后一张卡片时,整个轮播器消失在溢出中。

cardInnerNA 的 div 更改为 cardInner4,并链接到相应的 CSS 将展示整个效果是如何破坏的。

我尝试过调整位置方面的参数并添加 z-index,但都没有找到干净的解决方案,而不引起其他一系列的格式问题。

我对网页设计完全不熟悉,欢迎指出任何错误或者提供建议,告诉我如何追溯并纠正这个问题。

英文:

I am currently trying to implement a simple card flip animation to a SwiperJS carousel for a personal website.

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

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

var swiper = new Swiper(&quot;.slideContent&quot;, {
    slidesPerView: 3,
    spaceBetween: 30,
    centeredSlides: true,
    fade: &#39;true&#39;,
    variableWidth: &#39;true&#39;,
    pagination: {
      el: &quot;.swiper-pagination&quot;,
      clickable: true,
      dynamicBullets: true,
    },

    breakpoints: {
      0: {
        slidesPerView: 1,
      },

      520: {
        slidesPerView: 2,
      },

      950: {
        slidesPerView: 3,
      },

    },
  });

const button = document.querySelector(&#39;.eastButton&#39;);
const buttonBack = document.querySelector(&#39;.eastButtonBack&#39;);
const card = document.querySelector(&#39;.cardInner&#39;);


button.addEventListener(&#39;click&#39;, function() {
  card.classList.toggle(&#39;is-flipped&#39;);
});

buttonBack.addEventListener(&#39;click&#39;, function() {
  card.classList.toggle(&#39;is-flipped&#39;);
});


const button2 = document.querySelector(&#39;.fogesButton&#39;);
const buttonBack2 = document.querySelector(&#39;.fogesButtonBack&#39;);
const card2 = document.querySelector(&#39;.cardInner2&#39;);

button2.addEventListener(&#39;click&#39;, function() {
  card2.classList.toggle(&#39;is-flipped&#39;);
});

buttonBack2.addEventListener(&#39;click&#39;, function() {
  card2.classList.toggle(&#39;is-flipped&#39;);
});

const button3 = document.querySelector(&#39;.sephButton&#39;);
const buttonBack3 = document.querySelector(&#39;.sephButtonBack&#39;);
const card3 = document.querySelector(&#39;.cardInner3&#39;);

button3.addEventListener(&#39;click&#39;, function() {
  card3.classList.toggle(&#39;is-flipped&#39;);
});

buttonBack3.addEventListener(&#39;click&#39;, function() {
  card3.classList.toggle(&#39;is-flipped&#39;);
});

const button4 = document.querySelector(&#39;.davidButton&#39;);
const buttonBack4 = document.querySelector(&#39;.davidButtonBack&#39;);
const card4 = document.querySelector(&#39;.cardInner4&#39;);

button4.addEventListener(&#39;click&#39;, function() {
  card4.classList.toggle(&#39;is-flipped&#39;);
});

buttonBack4.addEventListener(&#39;click&#39;, function() {
  card4.classList.toggle(&#39;is-flipped&#39;);
});

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

*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: Arial, Helvetica, sans-serif;
}

body {
    height: 100%;
    width: 100%;
    color: #fff;
    background: #000;
}


.bodyContainer {
    min-height: 100vh; 
    display: flex;
    align-items: center;
    justify-content: center;
    background-image: url(/images/Texture_BG.jpg);
}

.slideContainer {
    max-width: 1120px;
    min-width: 560px;
    width: 100%;
    padding: 40px 0;
}

.slideContent {
    margin: 0 40px;
    overflow: hidden;
    border-radius: 25px;
}

.card {
    width: 300px;
    border-radius: 25px;
    background-color: #121212;

}

.cardInner {
    width: 100%;
    height: 100%;
    transition: transform 1s;
    transform-style: preserve-3d;
    cursor: pointer;
    position: absolute;
}


.cardInner.is-flipped {
    transform: rotateY(180deg);
}

.cardFace {
    position: absolute;
    width: 100%;
    height: 100%;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    border-radius: 25px;
    box-shadow: 0px 0px 10px 0px #2c2c2c;
}


.frontFace {
    display: flex;
    position: relative;
    height: 100%;
    width: 100%;
    flex-direction: column;
    align-items: center;
    background-color: #121212;
}



.imageContent, .cardContent {
    display: flex;  
    flex-direction: column;
    align-items: center;
    padding: 10px 14px;
}

.imageContent {
    position: relative;
    row-gap: 5px;
}

.overlay {
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    width: 100%;
    background-color: #121212;
    border-radius: 25px 25px 25px 25px;
}

.cardImg {
    position: relative;
    height: 100%;
    width: 100%;
    border-radius: 10px;
    margin: 10px;
}


.cardImg .avatarImg {
    height: 100%;
    width: 100%;
    object-fit: cover;
    border-radius: 20px;
    border: 2px solid #000;
}

.name {
    font-size: 30px;
    font-weight: 500;
    color: #fff;
    margin: 10px;
}

.button{
    border: none;
    font-size: 22px;
    color: #fff;
    padding: 8px 16px;
    background-color: #2c2c2c;
    border-radius: 8px;
    margin: 14px;
    cursor: pointer;
    transition: all 0.3s ease;
}

.button:hover {
    background: #454545;
}


.backFace {
    display: flex;
    position: relative;
    height: 100%;
    width: 100%;
    flex-direction: column;
    align-items: center;
    background-color: #121212;
    transform: rotateY(180deg);
}

.nameBack {
    font-size: 30px;
    font-weight: 500;
    color: #fff;
    margin: 10px;
    opacity: 0;
}


.cardInner2 {
    width: 100%;
    height: 100%;
    transition: transform 1s;
    transform-style: preserve-3d;
    cursor: pointer;
    position: absolute;
}


.cardInner2.is-flipped {
    transform: rotateY(180deg);
}


.cardInner3 {
    width: 100%;
    height: 100%;
    transition: transform 1s;
    transform-style: preserve-3d;
    cursor: pointer;
    position: absolute;
}


.cardInner3.is-flipped {
    transform: rotateY(180deg);
}

.cardInner4 {
    width: 100%;
    height: 100%;
    transition: transform 1s;
    transform-style: preserve-3d;
    cursor: pointer;
    position: absolute;
}


.cardInner4.is-flipped {
    transform: rotateY(180deg);
}

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

&lt;head&gt;
&lt;link
rel=&quot;stylesheet&quot;
href=&quot;https://cdn.jsdelivr.net/npm/swiper@9/swiper-bundle.min.css&quot;
/&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/swiper@9/swiper-bundle.min.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;div class=&quot;bodyContainer&quot;&gt;
&lt;div class=&quot;slideContainer swiper&quot;&gt;
&lt;div class=&quot;slideContent&quot;&gt;
&lt;div class=&quot;cardWrapper swiper-wrapper&quot;&gt;
&lt;div class=&quot;card swiper-slide&quot; id=&quot;eastCard&quot;&gt;
&lt;div class=&quot;cardInner&quot;&gt;
&lt;div class=&quot;cardFace frontFace&quot;&gt;
&lt;div class=&quot;imageContent&quot;&gt;
&lt;span class=&quot;overlay&quot;&gt;&lt;/span&gt;
&lt;div class=&quot;cardImg&quot;&gt;
&lt;img src=&quot;https://media.istockphoto.com/id/153435914/vector/crying-clown.jpg?s=612x612&amp;w=0&amp;k=20&amp;c=LVdIuvtyrDWh1sTERNIh5HF0wtWFNU2ZtpJXa8vhOUA=&quot; alt=&quot;&quot; class=&quot;avatarImg&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cardContent&quot;&gt;
&lt;h2 class=&quot;name&quot;&gt; 1 &lt;/h2&gt;
&lt;button class=&quot;button eastButton&quot; &gt; More &lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cardFace backFace&quot;&gt;
&lt;div class=&quot;imageContent&quot;&gt;
&lt;span class=&quot;overlay&quot;&gt;&lt;/span&gt;
&lt;div class=&quot;cardImg&quot;&gt;
&lt;img src=&quot;https://media.istockphoto.com/id/153435914/vector/crying-clown.jpg?s=612x612&amp;w=0&amp;k=20&amp;c=LVdIuvtyrDWh1sTERNIh5HF0wtWFNU2ZtpJXa8vhOUA=&quot; alt=&quot;&quot; class=&quot;avatarImg&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cardContent&quot;&gt;
&lt;h2 class=&quot;nameBack&quot;&gt; East &lt;/h2&gt;
&lt;button class=&quot;button eastButtonBack&quot; &gt; Back &lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;card swiper-slide&quot; id=&quot;fogesCard&quot;&gt;
&lt;div class=&quot;cardInner2&quot;&gt;
&lt;div class=&quot;cardFace frontFace&quot;&gt;
&lt;div class=&quot;imageContent&quot;&gt;
&lt;span class=&quot;overlay&quot;&gt;&lt;/span&gt;
&lt;div class=&quot;cardImg&quot;&gt;
&lt;img src=&quot;https://media.istockphoto.com/id/153435914/vector/crying-clown.jpg?s=612x612&amp;w=0&amp;k=20&amp;c=LVdIuvtyrDWh1sTERNIh5HF0wtWFNU2ZtpJXa8vhOUA=&quot; alt=&quot;&quot; class=&quot;avatarImg&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cardContent&quot;&gt;
&lt;h2 class=&quot;name&quot;&gt; 2 &lt;/h2&gt;
&lt;button class=&quot;button fogesButton&quot; &gt; More &lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cardFace backFace&quot;&gt;
&lt;div class=&quot;imageContent&quot;&gt;
&lt;span class=&quot;overlay&quot;&gt;&lt;/span&gt;
&lt;div class=&quot;cardImg&quot;&gt;
&lt;img src=&quot;https://media.istockphoto.com/id/153435914/vector/crying-clown.jpg?s=612x612&amp;w=0&amp;k=20&amp;c=LVdIuvtyrDWh1sTERNIh5HF0wtWFNU2ZtpJXa8vhOUA=&quot; alt=&quot;&quot; class=&quot;avatarImg&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cardContent&quot;&gt;
&lt;h2 class=&quot;nameBack&quot;&gt; Foges &lt;/h2&gt;
&lt;button class=&quot;button fogesButtonBack&quot; &gt; Back &lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;card swiper-slide&quot; id=&quot;sephCard&quot;&gt;
&lt;div class=&quot;cardInner3&quot;&gt;
&lt;div class=&quot;cardFace frontFace&quot;&gt;
&lt;div class=&quot;imageContent&quot;&gt;
&lt;span class=&quot;overlay&quot;&gt;&lt;/span&gt;
&lt;div class=&quot;cardImg&quot;&gt;
&lt;img src=&quot;https://media.istockphoto.com/id/153435914/vector/crying-clown.jpg?s=612x612&amp;w=0&amp;k=20&amp;c=LVdIuvtyrDWh1sTERNIh5HF0wtWFNU2ZtpJXa8vhOUA=&quot; alt=&quot;&quot; class=&quot;avatarImg&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cardContent&quot;&gt;
&lt;h2 class=&quot;name&quot;&gt; 3 &lt;/h2&gt;
&lt;button class=&quot;button sephButton&quot; &gt; More &lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cardFace backFace&quot;&gt;
&lt;div class=&quot;imageContent&quot;&gt;
&lt;span class=&quot;overlay&quot;&gt;&lt;/span&gt;
&lt;div class=&quot;cardImg&quot;&gt;
&lt;img src=&quot;https://media.istockphoto.com/id/153435914/vector/crying-clown.jpg?s=612x612&amp;w=0&amp;k=20&amp;c=LVdIuvtyrDWh1sTERNIh5HF0wtWFNU2ZtpJXa8vhOUA=&quot; alt=&quot;&quot; class=&quot;avatarImg&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cardContent&quot;&gt;
&lt;h2 class=&quot;nameBack&quot;&gt; Seph Nomad &lt;/h2&gt;
&lt;button class=&quot;button sephButtonBack&quot; &gt; Back &lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;card swiper-slide&quot; id=&quot;davidCard&quot;&gt;
&lt;div class=&quot;cardInnerNA&quot;&gt;
&lt;div class=&quot;cardFace frontFace&quot;&gt;
&lt;div class=&quot;imageContent&quot;&gt;
&lt;span class=&quot;overlay&quot;&gt;&lt;/span&gt;
&lt;div class=&quot;cardImg&quot;&gt;
&lt;img src=&quot;https://media.istockphoto.com/id/153435914/vector/crying-clown.jpg?s=612x612&amp;w=0&amp;k=20&amp;c=LVdIuvtyrDWh1sTERNIh5HF0wtWFNU2ZtpJXa8vhOUA=&quot; alt=&quot;&quot; class=&quot;avatarImg&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cardContent&quot;&gt;
&lt;h2 class=&quot;name&quot;&gt; 4 &lt;/h2&gt;
&lt;button class=&quot;button davidButton&quot; &gt; More &lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cardFace backFace&quot;&gt;
&lt;div class=&quot;imageContent&quot;&gt;
&lt;span class=&quot;overlay&quot;&gt;&lt;/span&gt;
&lt;div class=&quot;cardImg&quot;&gt;
&lt;img src=&quot;https://media.istockphoto.com/id/153435914/vector/crying-clown.jpg?s=612x612&amp;w=0&amp;k=20&amp;c=LVdIuvtyrDWh1sTERNIh5HF0wtWFNU2ZtpJXa8vhOUA=&quot; alt=&quot;&quot; class=&quot;avatarImg&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cardContent&quot;&gt;
&lt;h2 class=&quot;nameBack&quot;&gt; Seph Nomad &lt;/h2&gt;
&lt;button class=&quot;button davidButtonBack&quot; &gt; Back &lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;swiper-pagination&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

For some reason, every card but the last one in the carousel I got to work with only minor formatting issues.

Attempting to apply the card flip css to the last card in the carousel makes the entire swiper disappear into the overflow.

Changing the div cardInnerNA to cardInner4 which links it to the corresponding css will demonstrate how the entire thing breaks.

I tried messing around with the position aspects and adding z-index but nothing gave me a clean fix without causing a multitude of other formatting issues.

I am completely new to web design so feel free to point out any mistakes or at least give me advice on how I could go about tracing back and correcting this issue.

答案1

得分: 1

I came up with a simple but not completely clean workaround for this issue.

我想出了一个简单但不完全干净的解决方法。

I added a 5th "card swiper-slide" div with id="invisibleCard"

我添加了一个带有id="invisibleCard"的第五个 "card swiper-slide" div

Invisible

4

This is just a copy-paste of the structure of the previous cards but then I styled it.

这只是前几张卡片结构的复制粘贴,然后我对其进行了样式设置。

#invisibleCard {
opacity: 0;
}

to make it invisible.

使其不可见。

Fixed version

修复后的版本

JSFiddle

This is the only fix I can think of for now but if someone discovers a better way please let me know!

这是我目前能想到的唯一修复方法,但如果有人发现更好的方法,请告诉我!

英文:

I came up with a simple but not completely clean workaround for this issue.

I added a 5th "card swiper-slide" div with id ="invisibleCard"

&lt;div class=&quot;card swiper-slide&quot; id=&quot;invisibleCard&quot;&gt;
&lt;div class=&quot;cardInner5&quot;&gt;
&lt;div class=&quot;cardFace frontFace&quot;&gt;
&lt;div class=&quot;imageContent&quot;&gt;
&lt;span class=&quot;overlay&quot;&gt;&lt;/span&gt;
&lt;div class=&quot;cardImg&quot;&gt;
&lt;img src=&quot;https://media.istockphoto.com/id/153435914/vector/crying-clown.jpg?s=612x612&amp;w=0&amp;k=20&amp;c=LVdIuvtyrDWh1sTERNIh5HF0wtWFNU2ZtpJXa8vhOUA=&quot; alt=&quot;&quot; class=&quot;avatarImg&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cardContent&quot;&gt;
&lt;h2 class=&quot;name&quot;&gt; Invisible &lt;/h2&gt;
&lt;button class=&quot;button davidButton&quot; &gt; More &lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cardFace backFace&quot;&gt;
&lt;div class=&quot;imageContent&quot;&gt;
&lt;span class=&quot;overlay&quot;&gt;&lt;/span&gt;
&lt;div class=&quot;cardImg&quot;&gt;
&lt;img src=&quot;https://media.istockphoto.com/id/153435914/vector/crying-clown.jpg?s=612x612&amp;w=0&amp;k=20&amp;c=LVdIuvtyrDWh1sTERNIh5HF0wtWFNU2ZtpJXa8vhOUA=&quot; alt=&quot;&quot; class=&quot;avatarImg&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cardContent&quot;&gt;
&lt;h2 class=&quot;nameBack&quot;&gt; 4 &lt;/h2&gt;
&lt;button class=&quot;button davidButtonBack&quot; &gt; Back &lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

This is just a copy-paste of the structure of the previous cards but then I styled it

#invisibleCard {
opacity: 0;
}

to make it invisible.

Fixed version

JSFiddle

This is the only fix I can think of for now but if someone discovers a better way please let me know!

huangapple
  • 本文由 发表于 2023年4月19日 17:11:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052716.html
匿名

发表评论

匿名网友

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

确定