$(window).resize() 运行缓慢

发布于 2024-09-09 06:35:46 字数 1097 浏览 2 评论 0原文

我有一个带有缩略图的照片幻灯片。下一个/上一个按钮根据窗口的大小出现和消失;如果缩略图超出窗口大小,则会出现按钮。如果没有,它们就会消失。我的问题是,有时它们不会出现,或者有几秒钟都不会出现。其他时候它们不会消失。有时效果很好。

我对 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 技术交流群。

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

发布评论

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

评论(5

微暖i 2024-09-16 06:35:46

可能是浏览器在调整窗口大小时必须执行的工作(即重新调整布局以响应不断变化的窗口大小),添加到您所做的 DOM 更改中,只会让事情陷入困境。您可能会尝试等待用户交互完成,然后再触发代码。

$(window).resize((function() {
  var timeout = null;
  return function() {
    if (timeout) clearTimeout(timeout);
    timeout = setTimeout(findWidth, 250);
  };
})());

这会改变一些事情,以便您的代码在用户暂停或停止拖动窗口后 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.

$(window).resize((function() {
  var timeout = null;
  return function() {
    if (timeout) clearTimeout(timeout);
    timeout = setTimeout(findWidth, 250);
  };
})());

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.

萌︼了一个春 2024-09-16 06:35:46

加载时计算一次 screenWidth,因此您无需一遍又一遍地搜索 div#thumbs

Calculate screenWidth once upon load, so you don't have to search for div#thumbs over and over.

勿忘初心 2024-09-16 06:35:46

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.

韵柒 2024-09-16 06:35:46

创建一个变量 displayed 来存储按钮是否可见的当前状态。现在,在 resize 事件中,只有在它们隐藏时才执行 fadeIn 操作。

您还可以存储选定的元素,以免每次都进行选择。

// hide previous and next buttons
$prevNext = $('#prev, #next');
$prevNext.hide();

// get width of thumbnail list
var $thumbs = $('#thumbs');
var thumbsWidth = $('#thumbs ul').width();
var screenWidth;
var displayed = false;

// show/hide next/prev buttons
function buttonVisibility() {
    if (thumbsWidth + 225 > screenWidth) {
        if (!displayed) {
            displayed = true;
            $prevNext.fadeTo('fast', 0.5).hover(function () {
                $(this).fadeTo('fast', 1);
            }, function () {
                $(this).fadeTo('fast', 0.5);
            });
        }
    } else if (displayed) {
        displayed = false;
        $prevNext.fadeTo('fast', 0, function () {
            $(this).hide();
        });
    }
}

// find width of thumbnail window and show/hide next/prev buttons accordingly
function findWidth() {
    screenWidth = $thumbs.width();
    buttonVisibility();
}

// perform findWidth() on load and on window resize
findWidth();
$(window).resize(findWidth);

Create a variable displayed which stores the current state of whether the buttons are visible or not. Now in the resize event you only do fadeIn if they are hidden.

Also you can store the selected elements in order not to do the selection every time.

// hide previous and next buttons
$prevNext = $('#prev, #next');
$prevNext.hide();

// get width of thumbnail list
var $thumbs = $('#thumbs');
var thumbsWidth = $('#thumbs ul').width();
var screenWidth;
var displayed = false;

// show/hide next/prev buttons
function buttonVisibility() {
    if (thumbsWidth + 225 > screenWidth) {
        if (!displayed) {
            displayed = true;
            $prevNext.fadeTo('fast', 0.5).hover(function () {
                $(this).fadeTo('fast', 1);
            }, function () {
                $(this).fadeTo('fast', 0.5);
            });
        }
    } else if (displayed) {
        displayed = false;
        $prevNext.fadeTo('fast', 0, function () {
            $(this).hide();
        });
    }
}

// find width of thumbnail window and show/hide next/prev buttons accordingly
function findWidth() {
    screenWidth = $thumbs.width();
    buttonVisibility();
}

// perform findWidth() on load and on window resize
findWidth();
$(window).resize(findWidth);

心的位置 2024-09-16 06:35:46

En este ejemplo vemos como se ejecutara el codigo cada vez que se termine de redimensionar la ventana (在本例中,我们看到每次完成调整窗口大小时代码是如何执行的)

演示 1

JAVASCRIPT

$(document).ready(function ()
{
    iniciar();
});

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

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