简体   繁体   English

调整大小未触发的故障排除事件

[英]Trouble Shooting Event on Resize Not Firing

I am using the technique at this link to equalize the height of bootstrap carousel slides, so in the case of an uneven amount of text the slides do not cause the elements below them to bump up and down when advancing slides:我正在使用此链接中的技术来均衡引导轮播幻灯片的高度,因此在文本数量不均匀的情况下,幻灯片不会导致其下方的元素在推进幻灯片时上下颠簸:

https://snook.ca/archives/javascript/normalize-bootstrap-carousel-heights https://snook.ca/archives/javascript/normalize-bootstrap-carousel-heights

function normalizeSlideHeights() {
  $('.carousel').each(function() {
    var items = $('.carousel-item', this);
    items.css('min-height', 0);
    var maxHeight = Math.max.apply(null, items.map(function() {
      return $(this).outerHeight()
    }).get());
    items.css('min-height', maxHeight + 'px');
  })
}

$(window).on('load resize orientationchange', normalizeSlideHeights);

The first part of the code works great, all my carousel slides are the same height.代码的第一部分效果很好,我所有的轮播幻灯片的高度都相同。 The second part where it is looking for changes to the window size to re-adjust the slide height doesn't seem to work at all.它正在寻找更改 window 大小以重新调整幻灯片高度的第二部分似乎根本不起作用。 I tried editing the original code to just check for 'resize' to simplify it but did not see any difference.我尝试编辑原始代码以检查“调整大小”以简化它,但没有发现任何区别。

Any insights or ideas to a solution are greatly appreciated, thank you!非常感谢您对解决方案的任何见解或想法,谢谢!

Try plain JavaScript.试试普通的 JavaScript。 If something in jQuery isn't working for me, I always try plain JavaScript.如果 jQuery 中的某些东西对我不起作用,我总是尝试简单的 JavaScript。 An example is below:下面是一个例子:

window.onresize = function(){normalizeSlideHeights()}
window.onload = function(){normalizeSlideHeights()}


This WILL trigger the normalizeSlideHeights() function on resize and load, if the function exists.如果 function 存在,这将在调整大小和加载时触发normalizeSlideHeights() function。 If this does not work correctly, then the issue is with the function itself.如果这不能正常工作,那么问题出在 function 本身。
You could also put some consoleLog() commands to see wether the function actually fires at all on resize.您还可以放置一些consoleLog()命令来查看 function 是否真的在调整大小时触发。

I know this is not jQuery, but it's almost as short and easy to write.我知道这不是 jQuery,但它几乎一样简短且易于编写。 I hope my answer helps you.希望我的回答对你有所帮助。 I do not know for sure that this is the issue.我不确定这是否是问题所在。

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

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