简体   繁体   English

css 转换在调用 jQuery function 尝试更改 DOM 后不起作用

[英]css transition not working after calling jQuery function trying to change the DOM

I want to make tarot games.我想做塔罗牌游戏。 When the user click the card, the size of the card scales and the position of the card moves to the center of the screen.当用户点击卡片时,卡片的大小会缩放,卡片的 position 会移动到屏幕中央。 So I want to add a parent div on.flipping-card element after click event by calling jQuery wrap() function.所以我想通过调用 jQuery wrap() function 在点击事件后添加一个父 div on.flipping-card 元素。 Here's my js code:这是我的js代码:

Before I add this line: $(this).wrap("<div class='h-modal-bg'></div>");在我添加这一行之前: $(this).wrap("<div class='h-modal-bg'></div>");

The tarot cards flip transition effect works perfectly.塔罗牌翻转过渡效果完美。 But after adding that line, the transition effect just disappeared.但是加上那条线之后,过渡效果就消失了。 Can somebody tell me what's wrong with this code?有人可以告诉我这段代码有什么问题吗? Thank you, (When you run my code on snippets, it's better to resize the screen size to the mobile谢谢,(当您在片段上运行我的代码时,最好将屏幕大小调整为移动设备

 $(function() { $('.h-flipping-card').click(function(){ $(this).toggleClass('card-flipped'); $(this).closest('.card-frame').wrap("<div class='h-modal-bg h-modal-bg-show'></div>"); }); });
 *, *:before, *::after { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; }.h-flipping-card { position: relative; width: 96%; height: 0; padding-bottom: 144%; display: block; margin: 0 auto; perspective: 1000px; }.h-flipping-card-content { position: absolute; top: 0; left: 0; width: 100%; height: 100%; transform-style: preserve-3d; transition: transform 0.8s; }.h-flipping-card-front, .h-flipping-card-back { position: absolute; left: 0; top: 0; width: 100%; height: 100%; border-radius: 18px; box-shadow: 2px 1px 15px #8A8A8A; -webkit-backface-visibility: hidden; backface-visibility: hidden; }.h-flipping-card-front { transform: rotateY(180deg); }.h-flipping-card-back { background-color: brown; background-size: cover; background-repeat: no-repeat; background-position: center; }.h-flipping-card-front[data-tarot-opt='demo1'] { background-color: orange; background-size: 130% auto; background-repeat: no-repeat; background-position: center; }.h-flipping-card.card-flipped.h-flipping-card-content { transform: rotateY(180deg); }.h-modal-bg { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(33, 33, 33, 0.48); z-index: 10000; visibility: hidden; display: flex; flex-direction: column; justify-content: center; opacity: 0; -webkit-transition: all 0.3s ease-out; transition: all 0.3s ease-out; }.h-modal-bg-show { visibility: visible; opacity: 1; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <div class="card-frame" style="max-width: 320px; width: 50%; display: inline-block;"> <div class="h-flipping-card"> <div class="h-flipping-card-content"> <div class="h-flipping-card-back"></div> <div class="h-flipping-card-front" data-tarot-opt="demo1"></div> </div> </div> </div> </div>

This is because when you call $.wrap() , your original element is removed from the DOM, and then appended again.这是因为当您调用$.wrap()时,您的原始元素会从 DOM 中删除,然后再次附加。
Doing so, its computed style gets set directly to the one set by the class .card-flipped , and there is nothing to transition anymore, since for the CSS engine it's already in this flipped state.这样做,它的计算样式直接设置为 class .card-flipped设置的样式,并且不再有任何转换,因为对于 CSS 引擎,它已经在这个翻转的 Z9ED39E2EA931586B6EZEF8A 中。

You can see this related Q/A which explains a very similar situation.你可以看到这个相关的 Q/A解释了一个非常相似的情况。

The solution is thus to first wrap your element, then force a reflow so the CSS engine knows what's the current computed style of your element, and finally change its class, so the transition happens.因此,解决方案是首先包装您的元素,然后强制回流,以便 CSS 引擎知道您元素的当前计算样式是什么,最后更改其 class,这样就会发生转换。

$(function() {
    $('.h-flipping-card').click(function(){
        // first DOM manip
        $(this).closest('.card-frame').wrap("<div class='h-modal-bg h-modal-bg-show'></div>");
        document.body.offsetWidth; // force recalc
        $(this).toggleClass('card-flipped'); // now ask for transition
    });
});

 $(function() { $('.h-flipping-card').click(function(){ $(this).closest('.card-frame').wrap("<div class='h-modal-bg h-modal-bg-show'></div>"); document.body.offsetWidth; $(this).toggleClass('card-flipped'); }); });
 *, *:before, *::after { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; }.h-flipping-card { position: relative; width: 96%; height: 0; padding-bottom: 144%; display: block; margin: 0 auto; perspective: 1000px; }.h-flipping-card-content { position: absolute; top: 0; left: 0; width: 100%; height: 100%; transform-style: preserve-3d; transition: transform 0.8s; }.h-flipping-card-front, .h-flipping-card-back { position: absolute; left: 0; top: 0; width: 100%; height: 100%; border-radius: 18px; box-shadow: 2px 1px 15px #8A8A8A; -webkit-backface-visibility: hidden; backface-visibility: hidden; }.h-flipping-card-front { transform: rotateY(180deg); }.h-flipping-card-back { background-color: brown; background-size: cover; background-repeat: no-repeat; background-position: center; }.h-flipping-card-front[data-tarot-opt='demo1'] { background-color: orange; background-size: 130% auto; background-repeat: no-repeat; background-position: center; }.h-flipping-card.card-flipped.h-flipping-card-content { transform: rotateY(180deg); }.h-modal-bg { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(33, 33, 33, 0.48); z-index: 10000; visibility: hidden; display: flex; flex-direction: column; justify-content: center; opacity: 0; -webkit-transition: all 0.3s ease-out; transition: all 0.3s ease-out; }.h-modal-bg-show { visibility: visible; opacity: 1; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <div class="card-frame" style="max-width: 320px; width: 50%; display: inline-block;"> <div class="h-flipping-card"> <div class="h-flipping-card-content"> <div class="h-flipping-card-back"></div> <div class="h-flipping-card-front" data-tarot-opt="demo1"></div> </div> </div> </div> </div>

I think the issue is when your click event trigger every time your wrap function is calling and so .card-frame is wrapping inside already wrapped div .我认为问题是每次您的 wrap function 正在调用时您的点击事件触发,因此.card-frame 正在包裹在已经包裹的 div中。

So I think you just want to wrap only once on first click.所以我认为你只想在第一次点击时包装一次。 So I tried something one click function check this所以我尝试了一键 function 检查这个

$(function() {
    $('.h-flipping-card').click(function(){
        $(this).toggleClass('card-flipped');
    });
    $('.h-flipping-card').one("click", function(){
        $(this).closest('.card-frame').wrap("<div class='h-modal-bg h-modal-bg-show'></div>");
    });
});

Here is my JSFiddle Link这是我的JSFiddle 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM