Javascript jQuery只执行一次if语句并展开元素

Javascript jQuery只执行一次if语句并展开元素,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有一个jQuery脚本,当用户进入页面的正确位置时,它应该使div具有固定的位置。 问题是,每次用户滚动到正确的位置时,都会添加一组新的html元素,而else语句unwrap()中的元素不起作用 jQuery(document).ready(function () { // This function will be executed when the user scrolls the page. jQuery(window).scroll (function(e) { // Get

我有一个jQuery脚本,当用户进入页面的正确位置时,它应该使div具有固定的位置。 问题是,每次用户滚动到正确的位置时,都会添加一组新的html元素,而else语句unwrap()中的元素不起作用

jQuery(document).ready(function () {
// This function will be executed when the user scrolls the page.
jQuery(window).scroll (function(e) {
    // Get the position of the location where the scroller starts.
    var scroller_anchor = jQuery(".get-started").offset().top;
    // Check if the user has scrolled and the current position is after the scroller start location and if its not already fixed at the top 
    if (jQuery(this).scrollTop() >= scroller_anchor && jQuery('.get-a-quote').css('position') != 'fixed' ) 
    {    // Change the CSS of the scroller to hilight it and fix it at the top of the screen.
        jQuery('.get-a-quote').css({

            'position': 'fixed',
            'top': '0px'
        });
        jQuery('#stiky-btn').wrapAll('<nav class="navbar navbar-default navbar-fixed-top"><div class="container"><div id="navbar" class="navbar-collapse collapse"><ul class="nav navbar-nav navbar-right"></ul></div></div></nav>');

        // Changing the height of the scroller anchor to that of scroller so that there is no change in the overall height of the page.
        jQuery('.get-started').css('height', '50px');
    } 
    else if (jQuery(this).scrollTop() < scroller_anchor && jQuery('.get-a-quote').css('position') != 'relative') 
    {    // If the user has scrolled back to the location above the scroller anchor place it back into the content.

        // Change the height of the scroller anchor to 0 and now we will be adding the scroller back to the content.
        jQuery('.get-started').css('height', '0px');
        jQuery("#stiky-btn").unwrap();

        // Change the CSS and put it back to its original position.
        jQuery('.get-a-quote').css({
            'position': 'relative'
        });
    }
});
});
jQuery(文档).ready(函数(){
//此功能将在用户滚动页面时执行。
jQuery(窗口)。滚动(函数(e){
//获取滚动条启动位置的位置。
var scroller_anchor=jQuery(“.get start”).offset().top;
//检查用户是否已滚动,当前位置是否在滚动器开始位置之后,以及是否尚未固定在顶部
if(jQuery(this).scrollTop()>=scroller\u锚点和jQuery('.get-a-quote').css('position')!='fixed')
{//将滚动条的CSS更改为hilight,并将其固定在屏幕顶部。
jQuery('.get-a-quote').css({
'位置':'固定',
“顶部”:“0px”
});
jQuery(“#stiky btn”).wrapAll(“
    ”); //将scroller锚定的高度更改为scroller锚定的高度,以便页面的总体高度不会发生变化。 jQuery('.get start').css('height','50px'); } else if(jQuery(this).scrollTop()
    有什么想法吗

    试试这个检查

    if($("#stiky-btn").parent('.navbar').length == 0){
       jQuery('#stiky-btn').wrapAll('<nav class="navbar navbar-default navbar-fixed-top"><div class="container"><div id="navbar" class="navbar-collapse collapse"><ul class="nav navbar-nav navbar-right"></ul></div></div></nav>');
    }
    
    if($(“#stiky btn”).parent('.navbar').length==0){
    jQuery(“#stiky btn”).wrapAll(“
      ”); }
      你能发布一个演示吗?作为旁注:由于ID在文档上下文中必须是唯一的,所以你不需要使用
      wrapAll()
      wrap()
      是演示中的一个更新问题,谢谢。是的,这是一个很好的观点!但是我想应该用它来展开元素(或者两者都用)我已经更新了演示,但是这个行为对我来说还是很奇怪的。