我如何更好地重新排序我的JavaScript代码?

时间:2022-04-16 02:59:46

i'd like to make my code neater and less confusing. here's the code:

我想让我的代码更整洁,更少混淆。这是代码:

  // NAV BAR  //
$(window).on("scroll resize", function() {
 if($(window).width() > 980) {
  if($(window).scrollTop() > 20) {
    //add black background
    $(".x-navbar").addClass("active");
    $(".x-navbar .desktop .x-nav li  a").addClass("small-bar");
  } 
  else {
    //remove background
    $(".x-navbar").removeClass("active");
    $(".x-navbar .desktop .x-nav li a").removeClass("small-bar");
  }
 }else{
     // if window width < 980
     //remove background
    $(".x-navbar").removeClass("active");
    $(".x-navbar .desktop .x-nav li a").removeClass("small-bar");
 }
 });

what it does is simply gets the width of the screen and as soon as the user scrolls down, it restyles the header by giving it a smaller height and a different background using the classes active and small-bar. i got the code here and there so it's kinda messy, isn't there a way to write it in less strings and make it more efficient?

它的作用就是获取屏幕的宽度,一旦用户向下滚动,它就会通过使用活动和小栏类给它一个较小的高度和不同的背景重新设置标题。我在这里和那里得到了代码所以它有点混乱,是不是有办法用更少的字符串写它并使其更有效?

3 个解决方案

#1


1  

Next time please use CodeReview for this kind or purposes (working code that needs a review).

下次请使用CodeReview用于此类或目的(需要审核的工作代码)。

Here's the review:

这是审查:

// 1. Store each selector into a variable
// 2. Create a method to do the job and use `.toggleClass()` with its second parameter
// 3. Work with events and calling to the method when necessary.

$(window).on("scroll resize", function() {
    var $win = $(this);
    var $target = $('.x-navbar');
    var $subselector = $('.desktop .x-nav li a', $target);

    /** 
     * @param {boolean} action Set to true to show, false to hide
     */
    var DoJob = function(action){
        $target.toggleClass('active', action);
        $subselector.toggleClass('small-bar', action);
    };

    if( $win.width() > 980 ){
        if( $win.scrollTop() > 20 ) DoJob(true);
        else DoJob(false);
    }
    else DoJob(false);
});

#2


1  

You can do both checks at once, to remove duplicate code:

您可以一次执行两项检查,以删除重复的代码:

// NAV BAR  //
var $window = $(window);
$window.on("scroll resize", function() {
  var showBG = $window.width() > 980 && $window.scrollTop() > 20;
  if (showBG) {
    //add black background
    $(".x-navbar").addClass("active");
    $(".x-navbar .desktop .x-nav li  a").addClass("small-bar");
  }
  else {
    //remove background
    $(".x-navbar").removeClass("active");
    $(".x-navbar .desktop .x-nav li a").removeClass("small-bar");
  }
 });

#3


0  

var $win = $(window);
var nav = $(".x-navbar");
var navLinks = nav.find(".desktop .x-nav li  a");

$win.on("scroll resize", function() {
   if($win.width() > 980 && $win.scrollTop() > 20) {
     nav.addClass("active")
     navLinks.addClass("small-bar");
   } 
   else if($win.width() <= 980){
     nav.removeClass("active")
     navLinks.removeClass("small-bar");
   }
}

#1


1  

Next time please use CodeReview for this kind or purposes (working code that needs a review).

下次请使用CodeReview用于此类或目的(需要审核的工作代码)。

Here's the review:

这是审查:

// 1. Store each selector into a variable
// 2. Create a method to do the job and use `.toggleClass()` with its second parameter
// 3. Work with events and calling to the method when necessary.

$(window).on("scroll resize", function() {
    var $win = $(this);
    var $target = $('.x-navbar');
    var $subselector = $('.desktop .x-nav li a', $target);

    /** 
     * @param {boolean} action Set to true to show, false to hide
     */
    var DoJob = function(action){
        $target.toggleClass('active', action);
        $subselector.toggleClass('small-bar', action);
    };

    if( $win.width() > 980 ){
        if( $win.scrollTop() > 20 ) DoJob(true);
        else DoJob(false);
    }
    else DoJob(false);
});

#2


1  

You can do both checks at once, to remove duplicate code:

您可以一次执行两项检查,以删除重复的代码:

// NAV BAR  //
var $window = $(window);
$window.on("scroll resize", function() {
  var showBG = $window.width() > 980 && $window.scrollTop() > 20;
  if (showBG) {
    //add black background
    $(".x-navbar").addClass("active");
    $(".x-navbar .desktop .x-nav li  a").addClass("small-bar");
  }
  else {
    //remove background
    $(".x-navbar").removeClass("active");
    $(".x-navbar .desktop .x-nav li a").removeClass("small-bar");
  }
 });

#3


0  

var $win = $(window);
var nav = $(".x-navbar");
var navLinks = nav.find(".desktop .x-nav li  a");

$win.on("scroll resize", function() {
   if($win.width() > 980 && $win.scrollTop() > 20) {
     nav.addClass("active")
     navLinks.addClass("small-bar");
   } 
   else if($win.width() <= 980){
     nav.removeClass("active")
     navLinks.removeClass("small-bar");
   }
}