滚动到动态突出显示的表行

时间:2012-08-23 17:04:24

标签: javascript scroll

当您到达当前月份的页面时,源代码中的javascript会突出显示今天所代表的行。如何让页面自动向下滚动到该行?页面是http://www.itsmyturnnow.com/HWC/BRP/08.htm,我给出的脚本是源代码。感谢。

var INTENDED_MONTH = 7 //August
// INTENDED_MONTH is zero-relative
now = new Date().getDate(),
rows = document.getElementById('scripture').rows;
if (new Date().getMonth() != INTENDED_MONTH) {
    // need a value here less than 1, or the box for the first of the month will be in Gold
    now = 0.5
};
for (var i = 0, rl = rows.length; i < rl; i++) {
    var cells = rows[i].childNodes;
    for (j = 0, cl = cells.length; j < cl; j++) {
        if (cells[j].nodeName == 'TD'
  && cells[j].firstChild.nodeValue != ''
  && cells[j].firstChild.nodeValue == now) {
            rows[i].style.backgroundColor = 'red' // 'ffff99' // '#ffd700' // TODAY - gold
        }
    }
}

2 个答案:

答案 0 :(得分:1)

在当前诗篇中使用JQuery:

var INTENDED_MONTH = 7 //August
// INTENDED_MONTH is zero-relative
now = new Date().getDate(),
rows = document.getElementById('scripture').rows;
if (new Date().getMonth() != INTENDED_MONTH) {
    // need a value here less than 1, or the box for the first of the month will be in Gold
    now = 0.5
};
for (var i = 0, rl = rows.length; i < rl; i++) {
    var cells = rows[i].childNodes;
    for (j = 0, cl = cells.length; j < cl; j++) {
        if (cells[j].nodeName == 'TD'
  && cells[j].firstChild.nodeValue != ''
  && cells[j].firstChild.nodeValue == now) {
            rows[i].style.backgroundColor = 'red' // 'ffff99' // '#ffd700' // TODAY - gold
            $('html,body').delay(1000).animate({scrollTop:rows[i].offsetTop}, 500);

        }

    }
}

JSFiddle:http://jsfiddle.net/QT3v5/22/

答案 1 :(得分:0)

以下代码不需要jQuery。它将ID应用于当前行,计算行的位置(以像素为单位),然后滚动到该位置。

var INTENDED_MONTH = 7 //August
// INTENDED_MONTH is zero-relative
now = new Date().getDate(),
rows = document.getElementById('scripture').rows;
if (new Date().getMonth() != INTENDED_MONTH) {
    // need a value here less than 1, or the box for the first of the month will be in Gold
    now = 0.5
};
for (var i = 0, rl = rows.length; i < rl; i++) {
    var cells = rows[i].childNodes;
    for (j = 0, cl = cells.length; j < cl; j++) {
        if (cells[j].nodeName == 'TD'
  && cells[j].firstChild.nodeValue != ''
  && cells[j].firstChild.nodeValue == now) {
            rows[i].style.backgroundColor = 'red' // 'ffff99' // '#ffd700' // TODAY - gold
            rows[i].setAttribute('id', 'currentRow');
            function findPos(obj) {
                var curtop = 0;
                if (obj.offsetParent) {
                    do {
                        curtop += obj.offsetTop;
                    } while (obj = obj.offsetParent);
                return [curtop];
                }
            }
            window.scroll(0,findPos(document.getElementById('currentRow')));
        }
    }
}

它目前正在为我工​​作。

Demo