$(window).resize() 运行缓慢
我有一个带有缩略图的照片幻灯片。下一个/上一个按钮根据窗口的大小出现和消失;如果缩略图超出窗口大小,则会出现按钮。如果没有,它们就会消失。我的问题是,有时它们不会出现,或者有几秒钟都不会出现。其他时候它们不会消失。有时效果很好。
我对 jQuery 和 JavaScript 还很陌生。有什么建议吗?
// hide previous and next buttons
$('#prev, #next').hide();
// get width of thumbnail list
var thumbsWidth = $('div#thumbs ul').width();
// show/hide next/prev buttons
function buttonVisibility() {
if (thumbsWidth + 225 > screenWidth) {
$('#prev, #next')
.fadeTo('fast', 0.5)
.hover(function(){
$(this).fadeTo('fast', 1);
}, function(){
$(this).fadeTo('fast', 0.5);
});
} else {
$('#prev, #next').fadeTo('fast', 0, function(){
$(this).hide();
});
}
}
// declare global screenWidth variable
var screenWidth
// find width of thumbnail window and show/hide next/prev buttons accordingly
function findWidth(){
screenWidth = $('div#thumbs').width();
buttonVisibility();
}
// perform findWidth() on load and on window resize
findWidth();
$(window).resize(function(){
findWidth();
});
I've got a photo slideshow with thumbnails. Next/Previous buttons appear and disappear based on the size of the window; if the thumbnails overflow the window size, the buttons appear. If not, they disappear. My problem is that, at times, they won't come up, or they won't come up for a couple of seconds. At other times they won't disappear. Sometimes it works fine.
I am still pretty new to jQuery and JavaScript. Any suggestions?
// hide previous and next buttons
$('#prev, #next').hide();
// get width of thumbnail list
var thumbsWidth = $('div#thumbs ul').width();
// show/hide next/prev buttons
function buttonVisibility() {
if (thumbsWidth + 225 > screenWidth) {
$('#prev, #next')
.fadeTo('fast', 0.5)
.hover(function(){
$(this).fadeTo('fast', 1);
}, function(){
$(this).fadeTo('fast', 0.5);
});
} else {
$('#prev, #next').fadeTo('fast', 0, function(){
$(this).hide();
});
}
}
// declare global screenWidth variable
var screenWidth
// find width of thumbnail window and show/hide next/prev buttons accordingly
function findWidth(){
screenWidth = $('div#thumbs').width();
buttonVisibility();
}
// perform findWidth() on load and on window resize
findWidth();
$(window).resize(function(){
findWidth();
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
可能是浏览器在调整窗口大小时必须执行的工作(即重新调整布局以响应不断变化的窗口大小),添加到您所做的 DOM 更改中,只会让事情陷入困境。您可能会尝试等待用户交互完成,然后再触发代码。
这会改变一些事情,以便您的代码在用户暂停或停止拖动窗口后 1/4 秒之前不会尝试执行任何操作。
It might be that the work the browser intrinsically has to do while the window is resized (that is, re-figure the layout in response to the changing window size), added to the DOM changes you're making, is just bogging things down. What you might try is waiting for the user interaction to finish before triggering your code.
That'll change things so that your code won't try and do anything until 1/4 second after the user pauses or stops dragging the window around.
加载时计算一次
screenWidth
,因此您无需一遍又一遍地搜索div#thumbs
。Calculate
screenWidth
once upon load, so you don't have to search fordiv#thumbs
over and over.onresize
事件触发的频率似乎跨浏览器不一致。您可能需要实现自定义机制来控制时间。The frequency in which the
onresize
event is fired seems to be inconsistent across browsers. You may need to implement a custom mechanism to control the timing.创建一个变量
displayed
来存储按钮是否可见的当前状态。现在,在resize
事件中,只有在它们隐藏时才执行fadeIn
操作。您还可以存储选定的元素,以免每次都进行选择。
Create a variable
displayed
which stores the current state of whether the buttons are visible or not. Now in theresize
event you only dofadeIn
if they are hidden.Also you can store the selected elements in order not to do the selection every time.
En este ejemplo vemos como se ejecutara el codigo cada vez que se termine de redimensionar la ventana (在本例中,我们看到每次完成调整窗口大小时代码是如何执行的)
演示 1
JAVASCRIPT
En este ejemplo vemos como se ejecutara el codigo cada vez que se termine de redimensionar la ventana (In this example we see how the code is executed every time you finish resizing the window)
DEMO 1
JAVASCRIPT