jquery按位置获取元素id并调整颜色

时间:2015-06-27 10:27:07

标签: javascript jquery event-handling

我正在尝试编写移动网络应用。 我有几个方块彼此相邻,我希望当用户滑动屏幕时,他的手指按下的所有方格都会改变它们的背景颜色。

我写了一个脚本,指出用户手指在屏幕中的x和y位置。

我的问题是: 1.如何获取用户手指在上面滑动的元素的id?

这是我的代码:

CSS

.b {
    width: 50px;
    height: 50px;
    display: inline-block;
    border: red 1px solid;
}

HTML

<div id="demo"></div>
<h3 id="statusdiv">Status x</h3>
<h3 id="statusdiv1">Status y</h3>

的javascript

function init() {
    createLoop();
    $('.b').bind('touchstart', StartDragSelect);
    $('.b').bind('touchenter', StartMoveSelect);
    $('.b').bind('touchstart', successA);
};

function createLoop() {
    var length = 5;
    var text = "";
    var demo = $("#demo")
    for (i = 0; i < length; i++) {
        var rowElement = $('<div class="a"></div>');
        demo.append(rowElement);
        for (var x = 0; x < length; x++) {
            createGridItem(rowElement, i, x);
        }
    }
}

function createGridItem(rootElement, i, x) {
    var pix = 10;
    var currItem = $('<div class="b" id="a' + i + x + '" style="top:' + i * pix + 'px; left: ' + x * pix + 'px;  background-position-x: -' + x * pix + 'px ; background-position-y:-' + i * pix + 'px";"></div>');
    $(rootElement).append(currItem);
}

function StartDragSelect(obj) {

    var id = obj.currentTarget.id;
    obj = obj.currentTarget;

    console.log(id);
    console.log(obj);

    $(obj).css({
        "background-color": "blue"
    });
}

function StartMoveSelect(obj, x, i) {

    var id = obj.currentTarget.id;
    obj = obj.currentTarget;

    console.log(id);
    console.log(obj);

    $(obj).css({
        "background-color": "blue"
    });
}

function successA() {
    var v1 = document.getElementById('a11');
    if (v1.style.backgroundColor == "blue") {
        alert("Hello! I am an alert box!");
    }
}

/**/

window.addEventListener('load', function () {

    var demo = document.getElementById('demo')
    var statusdiv = document.getElementById('statusdiv')
    var statusdiv1 = document.getElementById('statusdiv1')
    var startx = 0
    var starty = 0
    var distx = 0
    var disty = 0

    demo.addEventListener('touchstart', function (e) {
        var touchobj = e.changedTouches[0] // reference first touch point (ie: first finger)
        startx = parseInt(touchobj.clientX) // get x position of touch point relative to left edge of browser
        starty = parseInt(touchobj.clientY) // get y position of touch point relative to left edge of browser
        statusdiv.innerHTML = 'Status: touchstart<br> ClientX: ' + '-' + startx + 'px'
        statusdiv1.innerHTML = 'Status: touchstart<br> ClientX: ' + starty + 'px'
        e.preventDefault()
    }, false)

    demo.addEventListener('touchmove', function (e) {
        var touchobj = e.changedTouches[0] // reference first touch point for this event
        var distx = parseInt(touchobj.clientX)
        var disty = parseInt(touchobj.clientY)
        statusdiv.innerHTML = 'Status: touchmove<br> Horizontal distance traveled: ' + '-' + distx + 'px'
        statusdiv1.innerHTML = 'Status: touchmove<br> vertical distance traveled: ' + disty + 'px'
        e.preventDefault()
    }, false)

    demo.addEventListener('touchend', function (e) {
        var touchobj = e.changedTouches[0] // reference first touch point for this event
        statusdiv.innerHTML = 'Status: touchend<br> Resting x coordinate: ' + touchobj.clientX + 'px'
        statusdiv1.innerHTML = 'Status: touchend<br> Resting y coordinate: ' + touchobj.clientY + 'px'
        e.preventDefault()
    }, false)

}, false)

function move($(obj).css({
    "background-color": "blue"
});) {
    if ()
}

1 个答案:

答案 0 :(得分:0)

如果您知道x和y coordiantes,您应该能够使用elementFromPoint方法在此坐标下获取元素:

var element = document.elementFromPoint(x, y);