简体   繁体   English

如何在元素的slideUp()之前阻止滚动和主体高度,并在新元素的slideDown()之后移除CSS? -jQuery的

[英]How block scroll and height of body before slideUp() of element and remove css after slideDown() of new element ? - Jquery

I assure you that I'm going crazy. 我向你保证我快疯了。 I've tried hundreds of changes but I can not solve this problem. 我尝试了数百种更改,但无法解决此问题。

In practice I have a page with 3 tabs but only one tab can be displayed at a time. 实际上,我有一个包含3个标签的页面,但一次只能显示一个标签。 If you click on the button, to view the new tab, the old one will be a slideUp and after that the new tab will appear with a slideDown . 如果单击该按钮以查看新选项卡,则旧选项卡将为slideUp ,之后新选项卡将显示为slideDown

The problem is that the tabs have different heights, so when a tab disappears, for a few moments, the window will have a different height and an automatic scroll will occur. 问题在于选项卡的高度不同,因此,当选项卡消失时,片刻之后,窗口将具有不同的高度,并且会自动滚动。

I want to avoid this scroll but I do not know how ... 我想避免这种滚动,但我不知道如何...

 $(document).ready(function() { $(document).scrollTop(30); //se viene cliccato un altro pulsante "SCHEDA" $('#button-scheda-mappa, #button-scheda-vulcani, #button-scheda-storici').on('click', function() { var mostra_scheda = ($(this).attr('id')).substr(7); $('#scheda-mappa, #scheda-vulcani, #scheda-storici').not('#' + mostra_scheda).slideUp(800); ////chiudo le altre voci aperte e le "pulisco" (eccetto quella appena cliccata) $('#' + mostra_scheda).delay(500).slideDown(800); //mostro la tendina del form (che sarebbe l'elemento successivo a quello appena cliccato) }); // }); 
 #box-buttons { display: flex; justify-content: space-between; width: 400px; } #button-scheda-mappa, #button-scheda-vulcani, #button-scheda-storici { width: 50px; background-color: #000; color: #fff; cursor: pointer; } #schede { width: 100%; display: flex; flex-direction: row; margin-top: 12px; font-size: 16px; color: #fff; justify-content: space-between; } #scheda-mappa, #scheda-vulcani, #scheda-storici { display: flex; align-items: center; justify-content: center; flex-direction: column; width: calc(100% - 2px); height: auto; background-color: #a0a0a0; border: 1px solid #404040; position: relative; top: -1px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <br> <br> <div id="box-buttons"> <div id="button-scheda-mappa"> 1 </div> <div id="button-scheda-vulcani"> 2 </div> <div id="button-scheda-storici"> 3 </div> </div> <br> <br> <div id="schede"> <div id="scheda-mappa"> <div style="height: 3000px"> </div> </div> <div id="scheda-vulcani" style="display:none; width: calc(100% - 2px); height: auto; background-color: #a0a0a0; border: 1px solid #404040; position: relative; top: -1px"> <div style="height: 300px"> </div> </div> <div id="scheda-storici" style="display:none; width: calc(100% - 2px); height: auto; background-color: #a0a0a0; border: 1px solid #404040; position: relative; top: -1px"> <div style="height: 100px"> </div> </div> </div> 

Here the jsfiddle: https://jsfiddle.net/fgt9odc8/ 这是jsfiddle: https ://jsfiddle.net/fgt9odc8/

I strongly recommend looking at the jsfiddle because the effect of the scroll is not observed here with the snippet. 我强烈建议您查看jsfiddle,因为在此代码段中未观察到滚动效果。 In the code the page starts with 在代码中,页面以

      $(document).scrollTop(30);

and when click a new button (to open new tab) the scrollTop position change :( 当单击一个新按钮(以打开新选项卡)时,scrollTop位置会发生变化:(

I hope you can help me and sorry for my english. 希望您能帮助我,对不起我的英语。

I solve blocking scroll of body with overflow-y: hidden before slideUp of old tab and removing this css after slideDown of new tab. 我解决了使用overflow-y: hidden阻止主体 scroll的问题overflow-y: hidden在旧选项卡的slideUp之前overflow-y: hidden ,并在新选项卡的slideDown之后删除此CSS。 Also i saved position of window before to change the tab and making a scroll to this (old) position after the new tab is show. 另外,我在更改选项卡之前保存了window位置,并在显示新选项卡后滚动到此(旧)位置。

I hope that this answer can help someone... 我希望这个答案可以帮助某人...

$(document).ready(function() {

      $(document).scrollTop(30);

      //se viene cliccato un altro pulsante "SCHEDA"
      $('#button-scheda-mappa, #button-scheda-vulcani, #button-scheda-storici').on('click', function() {
        var mostra_scheda = ($(this).attr('id')).substr(7);
        var blocca_scroll= $(window).scrollTop();   //posizione della finestra prima del cambio scheda      
        $('body, html').css('overflow-y','hidden'); //blocco lo scroll del body

        $('#scheda-mappa, #scheda-vulcani, #scheda-storici').not('#' + mostra_scheda).slideUp(800); ////chiudo le altre voci aperte e le "pulisco" (eccetto quella appena cliccata)
        $('#' + mostra_scheda).delay(500).slideDown(800, function(){
            $(window).scrollTop(blocca_scroll);
            $('body, html').removeAttr('style');    //sblocco anche la possibilità di scrollare la pagina
        });); //mostro la tendina del form (che sarebbe l'elemento successivo a quello appena cliccato)

      });
      //
    });

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

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