【问题标题】:resize function firing multiple times调整大小函数多次触发
【发布时间】:2019-05-28 07:14:54
【问题描述】:

我正在使用 resize() 方法运行一个将返回的函数 各种视口大小,如 992,768 等。问题是 函数每次执行都会导致 DOM 出现故障。例子 在代码笔https://codepen.io/paul-solomon/pen/WBEgQe

//javascript

$(window).resize(function(){
nh_masonry.data.loadorder();
});


//css

.masonry_grid_quarter__container{
 margin-bottom: 8px;
 column-count:3;
 column-gap:8px;
}

@media screen and (max-width:992px){
.masonry_grid_quarter__container{
 column-count:2;
 }
}

@media screen and (max-width:768px){
 .masonry_grid_quarter__container{
  column-count:2;
  }
}

@media screen and (max-width:580px){
 .masonry_grid_quarter__container{
  column-count:1;
  }
 }

预期结果:调整大小函数在特定视口处执行 实际结果:多次执行 resize 会造成 DOM 故障。

【问题讨论】:

  • 嗨,保罗,让这个工作顺利吗?
  • 我没有任何运气。当 console.log() 时,似乎在调整浏览器窗口宽度的同时多次添加对象,还有其他建议吗?
  • Paul,我现在正在查看你的 codepen,你可以做的是有一个变量来跟踪视口的当前状态,让我为你纠正一些事情。
  • 嗨,保罗,在下面试试我的解决方案。我还更新了你的 codepen。

标签: javascript


【解决方案1】:

您需要一个变量来跟踪 DOM 的当前状态。

让我们创建一个名为currentViewPort 的变量,它的值可以是“desktop”、“tablet”或“mobile”。

当窗口调整大小时,我们将切换currentViewPort 的值。如果currentViewPort 已经匹配了我们的屏幕布局,那么我们将不会再次调用该函数。只有当我们进入不同的布局时才会调用它。

它正在运行:https://codepen.io/anon/pen/GaGboG?editors=1111

   var App = (function ($) {

   var nh_masonry = {};
   var currentViewPort = ""
   //sets initial reorder variable
   nh_masonry.vars = { reorder : false} 

   nh_masonry.onload = function () {
            // when the document is loaded sets the reorder variable to true
            // and also loads the order object method 
            nh_masonry.vars.reorder = !nh_masonry.vars.reorder;
            nh_masonry.data.loadorder();

     $(window).resize(function(){
           // re-set layout after resize
           if($( window ).width() <= 992 && $( window ).width() > 768 && currentViewPort !== "desktop"){

             currentViewPort = "desktop"
             console.log("desktop");
              nh_masonry.data.loadorder();
              // nh_masonry.responsive.tablet();
              // nh_masonry.responsive.mobile();

           } else if ($( window ).width() == 768 && currentViewPort !== "tablet"){

             currentViewPort = "tablet"
             console.log("tablet");
              nh_masonry.data.loadorder();
              // nh_masonry.responsive.tablet();
              // nh_masonry.responsive.mobile();

           } else if ($( window ).width() < 768 && $( window ).width() >= 380 && currentViewPort !== "mobile"){
            currentViewPort = "mobile"
             console.log("mobile");
              nh_masonry.data.loadorder();
              // nh_masonry.responsive.tablet();
              // nh_masonry.responsive.mobile();
           }
         })  
     };

【讨论】:

  • 我建议使用 else if,这样就不需要阅读所有内容来查看是否会发生多个
  • 该变量确实解决了多次触发问题,但是最后一个问题是 codepen 是否使 DOM 事件调整大小正确,注意到我必须刷新才能看到正确的从左到右加载顺序以进行响应式视图 -克里斯托弗·恩戈
  • @PaulSolomon 我不认为 codepen 在显示 DOM 事件调整大小方面做得很好。每当您调整 codepen 浏览器的大小时,它都会尝试在我们编写的内容之上做自己的事情。在您的本地项目上尝试一下,它应该符合预期。
猜你喜欢
  • 2010-10-14
  • 2021-08-14
  • 2014-04-03
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多