CSS样式覆盖使用jQuery无法正常工作

时间:2015-03-10 08:03:50

标签: javascript jquery html css

我正在尝试在Windows滚动条上修复我的共享图标的位置。所以我将位置固定在滚动事件上。它工作不正常。

  1. 固定的重要位置不起作用。
  2. 如果我在起点向上滚动(在滚动停止的位置),则不会复制默认值。 CSS已修改 JS:

    $(document).ready(function () {
    var shareLink = $('#article_tools');
    
      function setHeader(){
       if($(window).width() <= 990){
        var shareLink = $('#article_tools');
        if( shareLink.length > 0) {
            shareLink.prepend($(".social-share-custom"));
            shareLink.next().css({'margin-top': shareLink.outerHeight(true) + "px", 'float': 'left'});
        }
        else{
            var social_alone = $(".social-share-custom");
            $(".evt_detail_main").before(social_alone);
            social_alone.next().css('float', 'left');
        }
    
        $(".author-article-info, .orange_oval, .author-article-organization").wrapAll('<div class="author-details"></div>');
    }
    if ($(window).width()<768) {
        shareLink.prepend($(".social-share-custom"));
        $('.follow-login-wrapper').append($('#top-login'));
        $('.responsive-menu').append($("#simplemenu"));
        $('#logo-title').after($('#container-search'));
        $('#homemenu').after($('#primary').find('>ul>li'));
        $('#comments').before($('#sidebar-right').find('#sidebar-right-inner>#block-constant_contact-0'));
    }
    

    }

    $(window).resize(function(){     的setHeader();    });

    $(window).scroll(function(){
    if($(window).width()<=990) {
        if (shareLink.length > 0) {
            shareLink.next().css('margin-top', shareLink.outerHeight(true) + "px");
            shareLink.css("top", Math.max(0, 274 - $(this).scrollTop()));
            shareLink.css('background-color', 'white');
        }
        else{
            var social_alone = $(".social-share-custom");
            social_alone.next().css('margin-top', shareLink.outerHeight(true) + "px");
            social_alone.css("top", Math.max(0, 200 - $(this).scrollTop()));
            if(social_alone.css("top") == "0"){
                social_alone.attr('style','position: fixed !important');
            }
            social_alone.css({'background-color':'white','width':'64%'});
        }
    }
    

    });

    的setHeader(); });

  3. 的CSS:

         .social-share-custom {
         float: left;
         width: auto;
         position: static !important;
         margin: 1%
        }
    
    有些人这样做。

2 个答案:

答案 0 :(得分:1)

使用此:

$(".social-share-custom").css({"position":"fixed !important"});

而不是:

 social_alone.attr('style','position: fixed !important');

答案 1 :(得分:0)

替换

social_alone.attr('style','position: fixed !important');

social_alone.css({ position: fixed!important });

还要先尝试将!important放在首位,如果没有那么你就不需要它了。)

相关问题