简体   繁体   English

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

[英]how do i reorder my javascript code better?

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. 它所做的只是获取屏幕的宽度,并且当用户向下滚动时,它会通过使用active和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? 我到处都是代码,所以有点混乱,没有办法用更少的字符串编写代码并提高效率吗?

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");
  }
 });

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);
});
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");
   }
}

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

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