我有一个表格,当我单击表格时,我希望使用最近的单元格属性来显示模块以正确对齐模块。
<table id="calendar" style = "margin-top: 65px; width: 100%; border-collapse: collapse;">
<!--<thead>
<tr>
<th>Sun</th>
<th>Mon</th>
<th>Tue</th>
<th>Wed</th>
<th>Thu</th>
<th>Fri</th>
<th>Sat</th>
</tr>
</thead>-->
<tbody id="calendar-body" style = "border: 2px lightgray solid;">
</tbody>
</table>
//onclick function calculating where to place module
window.onclick = function(event) {
document.getElementById("module");
module.style.left = event.x;
module.style.top = event.y;
}
//made table with dom
let today = new Date();
let currentMonth = today.getMonth();
let currentYear = today.getFullYear();
let selectYear = document.getElementById("year");
let selectMonth = document.getElementById("month");
let months = ["January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
let monthAndYear = document.getElementById("monthAndYear");
var hello = document.getElementById("calendar");
hello.style.height = window.innerHeight - 65 + "px";
showCalendar(currentMonth, currentYear);
function next() {
currentYear = (currentMonth === 11) ? currentYear + 1 : currentYear;
currentMonth = (currentMonth + 1) % 12;
showCalendar(currentMonth, currentYear);
}
function previous() {
currentYear = (currentMonth === 0) ? currentYear - 1 : currentYear;
currentMonth = (currentMonth === 0) ? 11 : currentMonth - 1;
showCalendar(currentMonth, currentYear);
}
function showCalendar(month, year) {
let firstDay = (new Date(year, month)).getDay();
let daysInMonth = 32 - new Date(year, month, 32).getDate();
let tbl = document.getElementById("calendar-body"); // body of the calendar
// clearing all previous cells
tbl.innerHTML = "";
// filing data about month and in the page via DOM.
monthAndYear.innerHTML = months[month] + " " + year;
// creating all cells
let date = 1;
//creating individual cells, filing them up with data.
var daysInWeek = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
for (let i = 0; i < 5; i++) {
// creates a table row
let row = document.createElement("tr");
for (let j = 0; j < 7; j++) {
if(i === 0) {
let cell = document.createElement("td");
let celltext = document.createTextNode(daysInWeek[j]);
let br = document.createElement("br");
cell.appendChild(celltext);
cell.appendChild(br);
cell.style.border = "2px solid lightgray";
cell.style.color = "gray";
if(j >= firstDay) {
let celltext2 = document.createTextNode(date);
cell.appendChild(celltext2);
date++;
}else {
let celltext2 = document.createElement("br");
cell.appendChild(celltext2);
}
row.appendChild(cell);
}else if (date > daysInMonth) {
let cell = document.createElement("td");
let cellText = document.createTextNode("");
cell.style.border = "2px lightgray solid";
cell.style.color = "gray";
cell.appendChild(cellText);
row.appendChild(cell);
}else {
let cell = document.createElement("td");
let cellText = document.createTextNode(date);
if (date === today.getDate() && year === today.getFullYear() && month === today.getMonth()) {
cell.style.backgroundColor = "yellow";
} // color today's date
cell.style.border = "2px lightgray solid";
cell.style.color = "gray";
cell.appendChild(cellText);
row.appendChild(cell);
date++;
} //end of condition statements
}
tbl.appendChild(row);
}//end of both fors
}//end of show calendar_today
var table = document.getElementById("calendar-body");
</script>
这段代码的问题是,有时模块会出现在屏幕外,为了抵消这种情况,我想获得在表格中单击的位置,并获取最近的单元格的x和y值,因此可以相应地放置模块。
我不想要最近的单元格的索引,而是想要单元格的实际left
和top
属性。
尝试使用getBoundingClientRect
https://developer.mozilla.org/zh-CN/docs/Web/API/Element/getBoundingClientRect
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句