jquery 触发问题

发布于 2024-08-05 20:07:12 字数 1387 浏览 11 评论 0原文

我正在使用一个函数来均衡从数据库返回的 li 标签的高度。我还使用 jquery 根据它们在行中的位置来分配某些新类。

基本上我的问题是 jquery 语句的定位部分总是有效,但等高部分有时不会触发,事实上它通常不适用于空缓存 - 但它总是会在刷新或第二次访问页面时触发。

现场示例在这里链接在这里!(让我知道它是否适合您!)

下面列出了我正在使用的 jquery 代码。

    jQuery.fn.equalCols = function(){
    //Array Sorter
    var sortNumber = function(a,b){return b - a;};
    var heights = [];
    //Push each height into an array
    $(this).each(function(){
        heights.push($(this).height());
    });
    heights.sort(sortNumber);
    var maxHeight = heights[0];
    return this.each(function(){
        //Set each column to the max height
        $(this).css({'height': maxHeight});
    });
};

jQuery(document).ready(function($) {
        $(".thumbs").each(function(i) { 
        if (i % 4 == 0) 
           $(this).addClass("start");
        if (i % 4 == 3) 
           $(this).addClass("end");
    });
    $(".thumbs").each(function(i) { 
        if (i % 3 == 0) 
           $(this).addClass("start");
        if (i % 3 == 2) 
           $(this).addClass("end");
           });
    $(".event_thumbs").each(function(i) { 
        if (i % 2 == 0) 
           $(this).addClass("start");
           });
$('.thumbs, .end, .start').equalCols();
});

如果您对实现目标的代码或方法有更好的建议,请大声喊出来,或者如果您知道如何使其发挥作用 - 我爱你。

I am using a function to equalise the height of li tags returned from a database. I am also using jquery to assign certain li new classes dependant on their position in a row.

Basocally my problem is that the positioning part of the jquery statement always works but the equal heights part will sometimes not fire, in fact it generally doesn't work on an empty cache - it will however always fire on refresh or second visit to the page.

Live example here Link here!(let me know if it works for you!)

The jquery code I am using is listed below.

    jQuery.fn.equalCols = function(){
    //Array Sorter
    var sortNumber = function(a,b){return b - a;};
    var heights = [];
    //Push each height into an array
    $(this).each(function(){
        heights.push($(this).height());
    });
    heights.sort(sortNumber);
    var maxHeight = heights[0];
    return this.each(function(){
        //Set each column to the max height
        $(this).css({'height': maxHeight});
    });
};

jQuery(document).ready(function($) {
        $(".thumbs").each(function(i) { 
        if (i % 4 == 0) 
           $(this).addClass("start");
        if (i % 4 == 3) 
           $(this).addClass("end");
    });
    $(".thumbs").each(function(i) { 
        if (i % 3 == 0) 
           $(this).addClass("start");
        if (i % 3 == 2) 
           $(this).addClass("end");
           });
    $(".event_thumbs").each(function(i) { 
        if (i % 2 == 0) 
           $(this).addClass("start");
           });
$('.thumbs, .end, .start').equalCols();
});

If you have an better suggestions for the code or methods to achieve the aim please shout out or if you know how to make it work - I love you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人间☆小暴躁 2024-08-12 20:07:12

也许是在最后一个循环中没有将其包装在 jQuery 中的问题?此外,您只需要跟踪迭代时看到的最大值。将高度放入数组并对其进行排序似乎效率很低。

jQuery.fn.equalCols = function(){
    var maxHeight = 0;
    $(this).each(function(){
        var height = $(this).height();
        if (height && height > maxHeight) maxHeight = height;
    });

    return $(this).each(function(){
        //Set each column to the max height
        $(this).css({'height': maxHeight});
    });
};

更新:由于页面上有图像,因此仅仅等待文档加载是不够的,您还需要等待图像加载。或者,更好的是,在加载每个图像后简单地进行均衡——这样您就一定会在所有图像加载后执行此操作。由于如果在应用图像之前加载图像,则不会调用加载处理程序(例如,它可能位于用户的缓存中,并且加载速度比 javascript 执行速度更快),因此设置一个计时器以在指定的时间后触发 equalCols 函数涵盖没有为任何图像调用加载处理程序的情况的时间量。将加载处理程序保留在图像上,然后如果您将源更改为图像,它将被重新应用并且页面再次均衡。

 $(function() {
     $('img').load( function() { /* equalize after every image load */
          equalize();
     });
     setTimeout( equalize, 2000 );  /* wait 2 secs, then equalize */
 });

 function equalize() {
     $('.thumbs, .end, .start').equalCols();
 }

Perhaps an issue with not wrapping this in jQuery on the last loop? Also, you only need to keep track of the maximum seen so far as you iterate. Putting the heights into an array and sorting it seems inefficient.

jQuery.fn.equalCols = function(){
    var maxHeight = 0;
    $(this).each(function(){
        var height = $(this).height();
        if (height && height > maxHeight) maxHeight = height;
    });

    return $(this).each(function(){
        //Set each column to the max height
        $(this).css({'height': maxHeight});
    });
};

Update: Since you have images on the page, it's not enough to wait until the document is loaded, you also need to wait until the images are loaded as well. Or, better yet, simply equalize after every image is loaded -- that way you're certain to do so after all of them are. Since the load handler won't be called if the image is loaded before it is applied (e.g., it may be in the user's cache and load more quickly than the javascript executes), set up a timer to fire the equalCols function after a specified amount of time to cover the case where the load handler isn't invoked for any image. Leave the load handler on the image, then if you change the source to the image it will be reapplied and the page equalized again.

 $(function() {
     $('img').load( function() { /* equalize after every image load */
          equalize();
     });
     setTimeout( equalize, 2000 );  /* wait 2 secs, then equalize */
 });

 function equalize() {
     $('.thumbs, .end, .start').equalCols();
 }
自此以后,行同陌路 2024-08-12 20:07:12

我做了类似的事情,让自动高度根据类进行匹配...跨浏览器工作,尽管它可能需要更新或plunized。

 function level_it(elem_class)
 {
      var s = 0;
      var t  = 0;
      $('.'+elem_class).each(function(){
      if($(this).height() < s)
      {
         s = $(this).height();
      }
      if($(this).height() > t)
      {
           t = $(this).height();
      }
  });
  $('.'+elem_class).each(function(){
    $(this).height(t);
  });
}
$(document).ready(function(){
  level_it('pricing');
});

I did somehting like this for auto-heights to match based on class... works cross browser, though it may need to be updated or plunized.

 function level_it(elem_class)
 {
      var s = 0;
      var t  = 0;
      $('.'+elem_class).each(function(){
      if($(this).height() < s)
      {
         s = $(this).height();
      }
      if($(this).height() > t)
      {
           t = $(this).height();
      }
  });
  $('.'+elem_class).each(function(){
    $(this).height(t);
  });
}
$(document).ready(function(){
  level_it('pricing');
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文