简体   繁体   English

jQuery翻转插件切换功能在开关上失败

[英]jQuery flip plugin switch function fails on switch

I have a piece of code where I have two buttons and when I press one button I want a div to flip on the y-axis and when I press the other button I want the same div to flip on the x-axis. 我有一段代码,我有两个按钮,当我按下一个按钮时,我想要一个div在y轴上翻转,当我按下另一个按钮时,我希望相同的div在x轴上翻转。 this works just fine when I use the button for the y-axis even if the previous flip was on the x-axis. 即使前一个翻转位于x轴上,当我使用y轴按钮时,这也可以正常工作。 But when I want to switch from the y-axis to the x-axis I need to press the x button twice because the first time nothing happens. 但是当我想从y轴切换到x轴时,我需要按两次x按钮,因为第一次没有任何反应。

So desired behavior is: 所以期望的行为是:

click y button flip the div on the y-axis. 单击y按钮在y轴上翻转div。

click x button flip the div on the x-axis. 单击x按钮在x轴上翻转div。

current behavior is: 目前的行为是:

click y button div flips on the y-axis. 单击y按钮div翻转y轴。

click x button nothing happens click x button again div flips on the x-axis. 单击x按钮没有任何反应单击x按钮再次在x轴上翻转div。

I have no idea what the problem is, this is the best i got so far. 我不知道问题是什么,这是我到目前为止所做的最好的。 All help is appreciated. 所有帮助表示赞赏。

 var ax = 'x'; $(document).ready(function() { $('#card').flip({ trigger: 'manual', axis: 'x' }); $('#left').click(function() { if (ax != 'y') { $("#card").flip({ axis: 'y' }); $("#card").on('flip:change', function() { $('#card').flip('toggle'); }); ax = 'y'; } else { $("#card").flip('toggle'); } }); $('#right').click(function() { if (ax != 'x') { $("#card").flip({ axis: 'x' }); $("#card").on('flip:change', function() { $('#card').flip('toggle'); }); ax = 'x'; } else { $("#card").flip('toggle'); } }); }); 
 #card { position: fixed; left: 50px; top: 50px; width: 200px; height: 200px; } .front { width: 100%; height: 100%; background-color: red; } .back { width: 100%; height: 100%; background-color: yellow; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdn.rawgit.com/nnattawat/flip/master/dist/jquery.flip.min.js"></script> <button id="left">y</button> <button id="right">x</button> <div id="card"> <div class="front">Front content</div> <div class="back">Back content</div> </div> 

You over-complicated the problem. 你过分复杂了这个问题。 Here is a solution inspired by the documentation . 这是一个受文档启发的解决方案。 All you have to do is to call flip to change the axis on click on he buttons, and to listen on flip:change event which is fired every time you call flip to change the orientation in order to actually flip it. 您所要做的就是在点击按钮时调用flip来改变轴,并在flip:change调用:每当你调用flip来改变方向以实际翻转时flip:change事件。

 // Configure it to be manually flipped $("#card").flip({ trigger: 'manual' }); // Change the axis $('#left').click(function() { $("#card").flip({ axis: 'y' }); }); $('#right').click(function() { $("#card").flip({ axis: 'x' }); }); // Toggle it on change $("#card").on('flip:change', function() { $('#card').flip('toggle'); }); 
 #card { position: fixed; left: 50px; top: 50px; width: 200px; height: 200px; } .front { width: 100%; height: 100%; background-color: red; } .back { width: 100%; height: 100%; background-color: yellow; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdn.rawgit.com/nnattawat/flip/master/dist/jquery.flip.min.js"></script> <button id="left">y</button> <button id="right">x</button> <div id="card"> <div class="front">Front content</div> <div class="back">Back content</div> </div> 

Here you go with a solution 在这里,您可以使用解决方案

 var ax = 'x'; $(document).ready(function() { $('#card').flip({ trigger: 'manual', axis: 'x' }); $("#card").on('flip:change', function() { $('#card').flip('toggle'); }); $('#left').click(function() { if (ax != 'y') { $("#card").flip({ axis: 'y' }); ax = 'y'; } else { $("#card").flip('toggle'); } }); $('#right').click(function() { if (ax != 'x') { $("#card").flip({ axis: 'x' }); ax = 'x'; } else { $("#card").flip('toggle'); } }); }); 
 #card { position: fixed; left: 50px; top: 50px; width: 200px; height: 200px; } .front { width: 100%; height: 100%; background-color: red; } .back { width: 100%; height: 100%; background-color: yellow; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdn.rawgit.com/nnattawat/flip/master/dist/jquery.flip.min.js"></script> <button id="left">y</button> <button id="right">x</button> <div id="card"> <div class="front">Front content</div> <div class="back">Back content</div> </div> 

You should keep the flip:change outside the click event. 您应该保持flip:change在click事件之外进行flip:change

Hope this will help you. 希望这会帮助你。

You ended up binding the flip:change event multiple times with each click. 你最终绑定了flip:change每次点击多次flip:change事件。 That caused x to flip twice when clicked after Y was clicked twice. 在点击Y两次后单击时,这会导致x翻转两次。 Hence it looks like nothing is happening. 因此看起来没有任何事情发生。

You can see that happening when adding a debugger; 您可以在添加debugger;时看到这种情况debugger; at the start of $('#right').click(function() { and using the console to debug your code. $('#right').click(function() {的开头$('#right').click(function() {并使用控制台调试代码。

Anyway, adding the event binding ones only fixes it as that is all you need. 无论如何,添加事件绑定只会修复它,因为这就是你所需要的。

Removing all instance of $("#card").on('flip:change', function() { in your code and only adding it one time, fixes the problem. 删除所有$("#card").on('flip:change', function() {在你的代码中只添加一次,修复问题。

In this example I added it at the bottom of your code. 在这个例子中,我将它添加到代码的底部。

 var ax = 'y'; $(document).ready(function() { $('#card').flip({ trigger: 'manual', axis: 'y' }); $('#left').click(function() { //debugger; if (ax != 'y') { $("#card").flip({ axis: 'y' }); ax = 'y'; } else { $("#card").flip('toggle'); } }); $('#right').click(function() { //debugger; if (ax != 'x') { $("#card").flip({ axis: 'x' }); ax = 'x'; } else { $("#card").flip('toggle'); } }); // Only bind ones. $("#card").on('flip:change', function() { $('#card').flip('toggle'); }); }); 
 #card { position: fixed; left: 50px; top: 50px; width: 200px; height: 200px; } .front { width: 100%; height: 100%; background-color: red; } .back { width: 100%; height: 100%; background-color: yellow; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdn.rawgit.com/nnattawat/flip/master/dist/jquery.flip.min.js"></script> <button id="left">y</button> <button id="right">x</button> <div id="card"> <div class="front">Front content</div> <div class="back">Back content</div> </div> 

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

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