【问题标题】:Visual issue with css3 animation and jquery (hover flip card)css3 动画和 jquery 的视觉问题(悬停翻转卡)
【发布时间】:2022-01-30 21:21:10
【问题描述】:

自述:--> 我在悬停翻盖卡时遇到问题,如果您在悬停在 div 内时移动光标会连续触发并且会产生烦人的效果,所以这里是我需要:

1) 我想悬停翻盖卡片,即使您在悬停时将光标移到里面,也不会再次触发动画。

2) 我还需要在翻转卡中垂直居中 p 标签,margin-top 有效,margin:auto 无效,我将 2 个示例放入 sn-p,检查下一个类:

.card .front p {
    margin-top: 100px;
}
.card .back p {
    margin: auto;
}

那么为什么margin:auto 不工作?我不会使用margin-top,需要在中心的内容。这是工作的 sn-p 代码:

更新问题: 我还需要实现下一个:一次你将翻转卡片悬停动画必须完成,即使你悬停div,然后再次翻转到原始位置,我尝试了下一个但它不起作用:

$(".container").hover(function(){
            $(this).find(".card").toggleClass('flipped')
        }, function(){
            setTimeout(function(){ 
                $(this).find(".card").toggleClass('flipped')
            }, 1000);
    });

$(document).ready(function () {
    $(".card").hover(function(){
            $(this).toggleClass('flipped')
        }, function(){
            $(this).toggleClass('flipped')
    });
})
.container {
    width: 200px;
    height: 260px;
    position: relative;
    margin: 0 auto 40px;
    -webkit-perspective: 800px;
    -moz-perspective: 800px;
    -o-perspective: 800px;
    perspective: 800px;
    display: inline-block;
}
#main{
    border: 1px solid black;
}
button{
    width: 30%;
    height: 10%;
    margin-top: 100px;
    cursor: default;
}

.card {
    width: 100%;
    height: 100%;
    position: absolute;
    -webkit-transition: -webkit-transform 1s;
    -moz-transition: -moz-transform 1s;
    -o-transition: -o-transform 1s;
    transition: transform 1s;
    -webkit-transform-style: preserve-3d;
    -moz-transform-style: preserve-3d;
    -o-transform-style: preserve-3d;
    transform-style: preserve-3d;
    -webkit-transform-origin: right center;
    -moz-transform-origin: right center;
    -o-transform-origin: right center;
    transform-origin: right center;
}

.card.flipped {
    -webkit-transform: translateX( -100%) rotateY( -180deg);
    -moz-transform: translateX( -100%) rotateY( -180deg);
    -o-transform: translateX( -100%) rotateY( -180deg);
    transform: translateX( -100%) rotateY( -180deg);
}

.card div {
    height: 100%;
    width: 100%;
    color: white;
    text-align: center;
    font-weight: bold;
    position: absolute;
    -webkit-backface-visibility: hidden;
    -moz-backface-visibility: hidden;
    -o-backface-visibility: hidden;
    backface-visibility: hidden;
    cursor:pointer;
}

.card .front {
    background: red;
}
.card .front p {
    margin-top: 100px;
}
.card .back p {
    margin: auto;
}

.card .back {
    background: blue;
    -webkit-transform: rotateY( 180deg);
    -moz-transform: rotateY( 180deg);
    -o-transform: rotateY( 180deg);
    transform: rotateY( 180deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-fluid">
        <div id="main"><br>
        <section class="container">
            <div class="card">
                <div class="front"><p>Test</p></div>
                <div class="back">
                    <p>MyBack</p> 
                </div>
            </div>
        </section>
        </div>
    </div>

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    问题来自这样一个事实,因为您正在转换已悬停的元素,因此指针可能会脱离框,导致状态从hover 到none 并再次返回。

    您可以在 main div 上捕获 hover 事件。要对齐元素,最简单的方法是使用CSS Flexbox。 margin: auto 将不起作用,因为它默认将 margin-top 和 margin-bottom 设置为 0。

    以下是我所做的更改:

    $(document).ready(function() {
      $(".container").hover(function() {
        $(".card").toggleClass('flipped')
      }, function() {
        $(".card").toggleClass('flipped')
      });
    })
    
    .card .back {
      /*no changes*/
      display: flex;
      justify-content: center;
      align-items: center;
    }
    
    .card .front {
      /*no changes*/
      display: flex;
      justify-content: center;
      align-items: center;
    }
    

    还有一个工作示例:

    $(document).ready(function() {
      $(".container").hover(function() {
        $(".card").toggleClass('flipped')
      }, function() {
        $(".card").toggleClass('flipped')
      });
    })
    h1 {
      text-align: center;
    }
    
    .container {
      width: 200px;
      height: 260px;
      position: relative;
      margin: 0 auto 40px;
      perspective: 800px;
      display: inline-block;
    }
    
    #main {
      border: 1px solid black;
    }
    
    button {
      width: 30%;
      height: 10%;
      margin-top: 100px;
      cursor: default;
    }
    
    .card {
      width: 100%;
      height: 100%;
      position: absolute;
      transition: transform 1s;
      transform-style: preserve-3d;
      transform-origin: right center;
    }
    
    .card.flipped {
      transform: translateX( -100%) rotateY( -180deg);
    }
    
    .card div {
      height: 100%;
      width: 100%;
      color: white;
      text-align: center;
      font-weight: bold;
      position: absolute;
      backface-visibility: hidden;
      cursor: pointer;
    }
    
    .card .front {
      background: red;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    
    .card .back p {
      margin: auto;
    }
    
    .card .back {
      background: blue;
      transform: rotateY( 180deg);
      display: flex;
      justify-content: center;
      align-items: center;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="container-fluid">
      <div id="main"><br>
        <section class="container">
          <div class="card">
            <div class="front">
              <p>Test</p>
            </div>
            <div class="back">
              <p>MyBack</p>
            </div>
          </div>
        </section>
      </div>
    </div>

    怎么样?

    【讨论】:

    • 悬停很好,但为什么工作边距不能自动使元素居中?
    • margin: auto 不起作用,因为它默认将 margin-top 和 margin-bottom 设置为 0。
    • 感谢您的回答,最后一个问题,您能检查一下我更新的问题吗?
    • 我们应该在聊天中继续这个讨论:chat.stackoverflow.com/rooms/158959/…
    猜你喜欢
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 2015-02-09
    • 2023-04-01
    • 2011-11-21
    • 2019-09-03
    相关资源
    最近更新 更多