缺少:属性id(jquery)之后

时间:2012-07-09 12:42:57

标签: javascript jquery html

我在firebug控制台中收到此错误,无法解决原因。我的代码如下:

$(document).ready(function(){
    var header = $('h2#chase');
    var headerOffset = header.offset();
    header.css({
        "position" : "absolute",
        "left" : headerOffset.left+"px",
        "top" : headerOffset.top+"px"
    });

    var headerSize = {width: header.width(), height: header.height()};
    var headerBounds = {top: headerOffset.top, right: headerOffset.left + headerSize.width, bottom: headerOffset.top + headerSize.bottom, left: headerOffset.left};
    var windowBounds = {top: 0, right: $(window).width() - headerSize.width, bottom: $(window).height() - headerSize.height, left: 0};
    var maxLeft = $(window).width() - headerSize.width;
    var maxTop = $(window).height() - headerSize.height;

    var margin = 30;

    $(window).mousemove(function(e){
        headerOffset = header.offset();
        headerBounds = {top: headerOffset.top, right: headerOffset.left + headerSize.width, bottom: headerOffset.top + headerSize.height, left: headerOffset.left};
        var sides = whichSides(e.pageX, e.pageY);
        $.each(sides, function(key, value) {
            header.css({
                value.side : value.newValue+"px"
            })
        });
    });

    function whichSides(mouseX, mouseY) {
        var sides = [];
        var sideOffsets = {top: headerBounds.top - mouseY, right: mouseX - headerBounds.right, bottom: mouseY - headerBounds.bottom, left: headerBounds.left - mouseX};
        var newValues = {top: mouseY + margin, right: mouseX - margin, bottom: mouseY - margin, left: mouseX + margin};
        $.each(sideOffsets, function(key, value) {
            if (value < 30 && value >= 0) {
                sides.push({side: key, newValue: newValues[key]});
            }
        });
        return sides;
    }
});

错误萤火虫正在返回:

    missing : after property id (line 24, col 9)
    value.side : value.newValue+"px"

编辑:道歉我似乎发布了今天早上我写的草稿中的代码,我已经用真正错误的代码更新了它。

2 个答案:

答案 0 :(得分:5)

在这一点:

header.css({
    value.side : value.newValue+"px"
})

value.side不是位于:左侧的有效构造

我不确定你要做什么,但它应该更多的内容:

header.css({
    side : value.newValue+"px"
})

阅读完评论后,您可以这样做:

var o = {}

o[value.side] = value.newValue+'px';

header.css(o);

答案 1 :(得分:0)

正如Ben所指出的,你不能在对象文字表达式的左侧(键)中包含变量。您需要单独设置它:

function make_pair(key, value){
    var obj = {};
    obj[key] = value;
    return obj;
}

header.css( make_pair(value.side, value.newValue+"px") )