javascript - 强制 safari 重新绘制 DOM

标签 javascript jquery dom safari webkit

显然,Safari 在提交事件期间不会重新绘制 DOM。我的代码在提交处理程序中包含 ajax 函数。我无法将表单或提交按钮的操作更改为 <button> 。在 ajax 函数中,我计算进度条的宽度并相应地调整 div。这适用于 Chrome 和 FF。不在 Safari 中。当我停止浏览器时,进度条会显示。

我想知道如何强制 Safari 在我的 ajax 函数中重新绘制 DOM。它看起来像这样:

$('#webfileuploadform').bind('submit',function(){
    var $progress = $('<div id="upload-progress" class="upload-progress"></div>').appendTo($('#webfileuploadform')).append('<div class="progress-container"><div class="progress-info">uploading 0%</div><div class="progress-bar"></div></div>');
    function update_progress_bar(){
        $.ajax({
            dataType:"json",
            async: false,
            url: progress_url,
            success: function(data){


                $progress.show();  // This shows the progress bar. I imagine 
                                   // this is where I would want to re-paint the DOM


                if(data.status == "uploading"){
                    var total_progress = parseInt(data.uploaded) / parseInt(data.length);
                    var width = $progress.find('.progress-container').width();
                    var progress_width = Math.round(width * total_progress);
                    var progress_px = progress_width + 'px';
                    $progress.find('.progress-bar').animate({'width':progress_px});
                    $progress.find('.progress-info').text('uploading ' + parseInt(total_progress*100) + '%');
                } else if (data.status == "not-found") {
                    $progress.find('.progress-bar').animate({'width':'100%'});
                    $progress.find('.progress-info').text('uploading 100%');
                    stop = 'Flag for end-of-file';
                } else {
                    //console.log('Ajax Error');
                }
            },
            error: function(textStatus){
                console.log("ERROR: " + textStatus);
            }
        });

        // Check flag to see if user stopped the page to cancel the ajax requests
        if(typeof stop == 'undefined'){
            // Iterates the ajax requests
            setTimeout(function(){update_progress_bar()}, freq);
        } else {
            setTimeout(function(){
                $progress.animate({opacity:0.5},1000);
            },freq);
            $progress.find('.progress-info').text('Refresh the page to continue.');
        }
    }

    // Initiate the ajax requests
    setTimeout(function(){update_progress_bar()}, freq);

});

最佳答案

我建议使用forceDOMReflowJS 。在您的情况下,您的代码将如下所示。另外,您的代码并不完全按照我的预期工作,因此我也修复了您的代码。

$('#webfileuploadform').bind('submit',function(){
    var $progress = $('<div id="upload-progress" class="upload-progress"></div>');
    // I believe you forgot to separate your attachments section of the code from
    // the variable declaration
    $progress.appendTo($('#webfileuploadform')).append('<div class="progress-container"><div class="progress-info">uploading 0%</div><div class="progress-bar"></div></div>');
    function update_progress_bar(){
        $.ajax({
            dataType:"json",
            async: false,
            url: progress_url,
            success: function(data){
                $progress.show();
////////////////////////////////////////////////////////////////////////////////
                // Perform the reflow (even though I don't really see how it would change things)
                forceReflowJS( $progress[0] );
////////////////////////////////////////////////////////////////////////////////
                if(data.status == "uploading"){
                    var total_progress = parseInt(data.uploaded) / parseInt(data.length);
                    var width = $progress.find('.progress-container').width();
                    var progress_width = Math.round(width * total_progress);
                    var progress_px = progress_width + 'px';
                    $progress.find('.progress-bar').animate({'width':progress_px});
                    $progress.find('.progress-info').text('uploading ' + parseInt(total_progress*100) + '%');
                } else if (data.status == "not-found") {
                    $progress.find('.progress-bar').animate({'width':'100%'});
                    $progress.find('.progress-info').text('uploading 100%');
                    stop = 'Flag for end-of-file';
                } else {
                    //console.log('Ajax Error');
                }
            },
            error: function(textStatus){
                console.log("ERROR: " + textStatus);
            }
        });

        // Check flag to see if user stopped the page to cancel the ajax requests
        if(typeof stop == 'undefined'){
            // Iterates the ajax requests
            setTimeout(function(){update_progress_bar()}, freq);
        } else {
            setTimeout(function(){
                $progress.animate({opacity:0.5},1000);
            },freq);
            $progress.find('.progress-info').text('Refresh the page to continue.');
        }
    }

    // Initiate the ajax requests
    setTimeout(function(){update_progress_bar()}, freq);

});

关于javascript - 强制 safari 重新绘制 DOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16722539/

相关文章:

javascript - Html Canvas - 仅在 X 或 Y 轴上移动对象

javascript - div样式随动画变化?

javascript - 我的 Greasemonkey 脚本没有在页面上找到元素(链接)?

jQuery 通过自定义属性获取元素

javascript - 去掉 c3.js 上的中间线

javascript - Fiddle 有效但测试页无效(缺少 .ready 处理程序)

javascript - jQuery 文档中的 touchevent 事件函数

javascript - 当 asp :Textbox disabled 时禁用 DatePicker

javascript - 从 .ascx 文件调用 Javascript 函数

java - 在 Java 中比较 XML