简体   繁体   English

div“闪烁”,并在fadeOut / fadeIn上移动

[英]div “flickers” and Moves on fadeOut/fadeIn

I have the same problem that is described here: same problem (fiddle: original fiddle ) but I dont understand JS yet, I am learning as I code, so when my html code is little bit different as it is now, I dont know, how to apply the solution onto it. 我在这里描述了同样的问题同样的问题 (小提琴: 原始的小提琴 ),但是我还不了解JS,我正在学习代码,所以当我的html代码与现在有点不同时,我不知道,如何将解决方案应用到它。 Could You help me please with that? 你能帮我吗? When I see, how it should look, I can learn it then. 当我看到它的外观时,便可以学习它。

My code: 我的代码:

<div style="position: absolute; left: 50%;">
    <div style="position: relative; left: -50%;">
        <div id="101" style="display:none;"><!-- Content here --></div>
        <div id="102" style="display:none;"><!-- Content here --></div>
        <div id="103" style="display:none;"><!-- Content here --></div>
        <div id="104" style="display:none;"><!-- Content here --></div>
        <div id="105" style="display:none;"><!-- Content here --></div>
        <div id="106" style="display:none;"><!-- Content here --></div>
        <div id="107" style="display:none;"><!-- Content here --></div>
        <div id="108" style="display:none;"><!-- Content here --></div>
        <div id="109" style="display:none;"><!-- Content here --></div>
        <div id="110" style="display:none;"><!-- Content here --></div>
        <div id="111" style="display:none;"><!-- Content here --></div>
        <div id="112" style="display:none;"><!-- Content here --></div>
        <div id="113" style="display:none;"><!-- Content here --></div>
    </div>
</div>

<script type="text/javascript">

    $("#1").on('click', function() {
    $("#101").fadeToggle();
    $("#102,#103,#104,#105,#106,#107,#108,#109,#110,#111,#112,#113").fadeOut();
    });

    $("#2").on('click', function() {
    $("#102").fadeToggle();
    $("#101,#103,#104,#105,#106,#107,#108,#109,#110,#111,#112,#113").fadeOut();
    });

    // and so on, until #13 
</script>

Solution: 解:

$('#indexNav').click(function() {
$('#aboutContent').fadeOut('fast',function(){
    $('#indexContent').fadeIn('fast');
});
return false;
});
$('#aboutNav').click(function() {
$('#indexContent').fadeOut('fast',function(){
    $('#aboutContent').fadeIn('fast');
});
    return false;
});

Wow, thats a lot of code to review. 哇,多数民众赞成在很多代码进行审查。 I'm not positive this is going to solve your problem. 我不太肯定这会解决您的问题。 You don't have any hover events on this, so I assume flicker may be CSS related. 您对此没有任何悬停事件,因此我认为闪烁可能与CSS有关。

// better selector - you don't have markup for #1, #2, #3, etc.
// can you rework this to be $('.container div') or something based on real markup
$('div').on('click', function() {

  // get this element's id
  var id = $(this).attr('id');

  // find the corresponding div ID, which seems to be 100 greater than original ID
  $('#1'+id).fadeToggle()
      // find all siblings and hide them
      .siblings().fadeOut();

});

I've added a snippet so you can test it. 我添加了一个代码段,以便您可以对其进行测试。

 $(function() { // equivalent to onDomReady $('button').on('click', function() { // get this element's id var id = $(this).attr('id'); // find the corresponding div ID, which seems to be 100 greater than original ID $('#1' + id).fadeToggle() // find all siblings and hide them .siblings().fadeOut(); }); }); 
 div div div { border: 1px solid black; margin: 3px; background: red; } 
 <button id="01">01</button> <button id="02">02</button> <button id="03">03</button> <button id="04">04</button> <button id="05">05</button> <button id="06">06</button> <button id="07">07</button> <button id="08">08</button> <button id="09">09</button> <button id="10">10</button> <button id="11">11</button> <button id="12">12</button> <button id="13">13</button> <div style="position: absolute; left: 50%;"> <div style="position: relative; left: -50%;"> <div id="101" style="display: none;">101</div> <div id="102" style="display: none;">102</div> <div id="103" style="display: none;">103</div> <div id="104" style="display: none;">104</div> <div id="105" style="display: none;">105</div> <div id="106" style="display: none;">106</div> <div id="107" style="display: none;">107</div> <div id="108" style="display: none;">108</div> <div id="109" style="display: none;">109</div> <div id="110" style="display: none;">110</div> <div id="111" style="display: none;">111</div> <div id="112" style="display: none;">112</div> <div id="113" style="display: none;">113</div> </div> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

Update What you're suffering from is the moment of time when both divs (one fading out, and one fading in) are being toggled. 更新您所遭受的是切换两个div(一个淡入和一个淡入)的时间。 You'll need to position those divs absolutely to get around this. 您需要完全定位这些div才能解决此问题。 Then they're overlapping each other and you don't need to worry about the flicker. 然后它们彼此重叠,您无需担心闪烁。 That can have big impacts on layout. 这会对布局产生重大影响。 Then you get into calculating heights of those child divs and all sorts of stuff: https://jsfiddle.net/SbKQ3/29/ 然后,您将开始计算这些子div的高度和各种内容: https : //jsfiddle.net/SbKQ3/29/

How about that? 那个怎么样? I used promises to fadeToggle only when all elements were hidden, which prevented the flickering effect. 我仅在隐藏所有元素时使用了promisesfadeToggle ,这可以防止闪烁效果。

 $(function() { // equivalent to onDomReady $('button').on('click', function() { // get this element's id var id = $(this).attr('id'); // find the corresponding div ID, which seems to be 100 greater than original ID $.when($('#1' + id).siblings().fadeOut()).done(function() { $('#1' + id).fadeToggle(); }); }); }); 
 div div div { border: 1px solid black; margin: 3px; background: red; } 
 <button id="01">01</button> <button id="02">02</button> <button id="03">03</button> <button id="04">04</button> <button id="05">05</button> <button id="06">06</button> <button id="07">07</button> <button id="08">08</button> <button id="09">09</button> <button id="10">10</button> <button id="11">11</button> <button id="12">12</button> <button id="13">13</button> <div style="position: absolute; left: 50%;"> <div style="position: relative; left: -50%;"> <div id="101" style="display: none;">101</div> <div id="102" style="display: none;">102</div> <div id="103" style="display: none;">103</div> <div id="104" style="display: none;">104</div> <div id="105" style="display: none;">105</div> <div id="106" style="display: none;">106</div> <div id="107" style="display: none;">107</div> <div id="108" style="display: none;">108</div> <div id="109" style="display: none;">109</div> <div id="110" style="display: none;">110</div> <div id="111" style="display: none;">111</div> <div id="112" style="display: none;">112</div> <div id="113" style="display: none;">113</div> </div> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

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

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