简体   繁体   English

试图在滚动和窗口大小上隐藏徽标

[英]Trying to hide logo on scroll and on windows size

I am new to coding and are trying to get a function for my nav to hide the logo and become fixed at top, but can not make this code to work. 我是编码新手,并试图让我的导航功能隐藏徽标并固定在顶部,但无法使此代码工作。

function rekotex() {

  //Do something under 767 px.
  if (window.matchMedia) { 
    if(window.matchMedia("all and (max-width: 767px)").matches) {

        // Hide logo.
        jQuery("#logo-in-menu").hide();
    }

 } else { // Over 768px.

    // See if page is scrolled, if so hide Logo.
    var height = jQuery(window).scrollTop();

    if(height <= 1){
        jQuery("#logo-in-menu").show();
        jQuery(".header").css({"height":230});
    }

    else if(height > 1) {
        jQuery("#logo-in-menu").hide();
        jQuery(".header").css({"height":130});
    }

}

var doit;
    window.onresize = function(){
        clearTimeout(doit);
        doit = setTimeout(rekotex, 100);
    };
}

If you want to check if the user is scrolling the page you should use $(window).scroll(function (event), I THINK that what you want is this: 如果你想检查用户是否在滚动页面,你应该使用$(window).scroll(function(event),我认为你想要的是这个:

$( document ).ready(function() {
    if(window.matchMedia("all and (max-width: 767px)").matches) {
        //Do something under 767 px.
        // Hide logo.
        jQuery("#logo-in-menu").hide();
    } else {// Over 768px.
    function rekotex() {
    // See if page is scrolled, if so hide Logo.
    var height = jQuery(window).scrollTop();

    if(height <= 1){
        jQuery("#logo-in-menu").show();
        jQuery(".header").css({"height":230});
    }

    else if(height > 1) {
        jQuery("#logo-in-menu").hide();
        jQuery(".header").css({"height":130});
    }

}


var doit;
    $(window).scroll(function (event) {
        clearTimeout(doit);
        doit = setTimeout(rekotex, 100);
    });
}
});

When the page loads, checks if the width is under 768 and if so, hides the logo, otherwise declares the function and hides the logo if the user scrolls 页面加载时,检查宽度是否低于768,如果是,则隐藏徽标,否则声明该功能并在用户滚动时隐藏徽标

Also this is just a hunch, but is the setTimeout meant to be a fadeout animation? 这也只是一个预感,但是setTimeout意味着是一个淡出动画吗? If so, you should write the time in the .hide(). 如果是这样,你应该在.hide()中写下时间。

The easiest way to do this is using jQuery: 最简单的方法是使用jQuery:

       $(document).on( 'scroll', function(){
            $('#image').hide()
       });

Solved it with this code. 解决了这个代码。

#CSS
  #logo-in-menu { // Göm i mobil
    display: none !important;
  }

  .header {
    height: 75px !important;
  }

#JS
jQuery(window).scroll(function() {
    var height = jQuery(window).scrollTop();
    if(height <= 1){
       jQuery("#logo-in-menu").show();
       jQuery(".header").css({"height":180});
    }
    else if(height > 1) {
        jQuery("#logo-in-menu").hide();
        jQuery(".header").css({"height":75});
    }
});

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

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