javascript - 单击功能错误上的 WebKit 动画

标签 javascript css google-chrome safari webkit

我将以下代码设置为在单击某个对象时移动某些对象,但您会在 Safari 和 Chrome 中看到框的动画有点不正常,而 Firefox 显示正确。

有没有办法修复这个错误?

http://coreytegeler.com/jg/

    $(function(){
    $("#nav li").click(function() {
        $("#nav").css({
            'left' : $(this).position().left + 'px',
            'top' : $(this).position().top + 'px'
        })
        .animate({
             'margin-top' : '-175px',
              'margin-left' : '0px',
            'left' : '10px',
            'top' : '50%',
            'height' : '370px',
            'width' : '70px'
        }, 500, 'swing');

        $("#name").css({
            'top': $(this).position().top + 'px'
        })
        .animate({
            'top' : '100px'
        } , 500, 'swing');
    });

    $("#nav li#a").click(function() {
        $(".set#a").animate({
            'opacity' : '1' ,
            'top' : '50%',
             'margin-top' : '-200px'
            }, 500, 'swing');
    });

});

最佳答案

您遇到的是 webkit 处理将内联元素转换为固定元素的方式。无论如何,当您将元素更改为 fixed 时,它会将 left 默认为 0,即使您明确地告诉它不是这样。您可以在这里准备更多关于如何解决它的信息:Center a position:fixed element

基本上您需要将元素的左侧位置设置为 50%,然后计算元素宽度的 1/2 的负边距。

祝你好运,也许可以重写你的代码。您应该查看 JQuery 链接,因为您的某些代码是多余的。此外,由于您只针对一个元素,因此可以删除 .each() 因为不需要它们。只有当你想遍历一个可能返回多个元素的选择器时,你才会使用 .each 。在您的情况下,您的选择器仅针对一个元素。我稍微重写了您的代码,以提高可读性,减少冗余:

$(function(){
    $("#nav ul li").click(function() {
        $("#nav ul").css({
            'position' : 'fixed',
            'left' : $(this).position().left + 'px',
            'top' : $(this).position().top + 'px'
        })
        .animate({
            'left' : '10px',
            'top' : '50%',
            'margin-top' : '-140px',
            'height' : '280px',
            'width' : '70px'
        }, 500, 'swing');

        $("#name").css({
            'top': $(this).position().top + 'px'
        })
        .animate({
            'position' : 'fixed',
            'top' : '100px'
        } , 500, 'swing');
    });

    $("#nav ul li#a").click(function() {
        $(".set#a").animate({
            'opacity' : '1' ,
            'top' : '50%'}, 500, 'swing');
    });

});

关于javascript - 单击功能错误上的 WebKit 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18167959/

相关文章:

javascript - iOS chrome - 翻译页面时 xhr.timeout 抛出错误

javascript - 将表单中输入的值从字符串更改为整数 javascript

html - 不能使用特定宽度 - CSS 菜单

javascript - 从用户定义的 typescript 路径导出枚举导致找不到模块

javascript - 调整窗口大小时不隐藏 Canvas 外菜单

JQuery替换src属性子字符串

google-chrome - 仅在 Chrome 开发工具中使用 xhr 请求创建 har 文件

html - 我的网站在 Firefox 上看起来非常不同。无法查明原因

javascript - 求任意多项式函数在 x 处的正切

javascript - 将 infoWindows 添加到数组中的多个标记