Converting Top/Left Offset Coordinates into Day/Time
I built a grid and then used the following "showCoordinates" function to calculate positions on the grid of a draggable element.
function showCoordinates(divId)
{
var leftParent= $("#"+divId).parent().offset().left;
var topParent= $("#"+divId).parent().offset().top;
var left= $("#"+divId).offset().left;
var top= $(开发者_Python百科"#"+divId).offset().top;
var leftTableParent =$("#tableId").parent().offset().left;
var topTableParent =$("#tableId").parent().offset().top;
var leftTable =$("#tableId").offset().left;
var topTable =$("#tableId").offset().top;
var cellWidth =160;
var cellHeight =120;
var offsetDifferenceTop =41;
var offsetDifferenceLeft = -84;
var actualLeft =left-offsetDifferenceLeft;
var actualTop =top-offsetDifferenceTop;
var cellRow =actualTop /cellHeight;
var cellColumn =actualLeft / cellWidth;
var Day = ??
var Time = ??
$("#displayCoordinates").html("cell row: "+cellRow+" | cell column: "+cellColumn+" | Time: "+Time+" | Day: "+Day+" <br />");
}
I would like to convert the cell coordinates to day (7 days - sunday to saturday) and time (this maybe more complicated. Im thinking day would be more simple using if..else statements (if cellColumn = 1 then Day = Sunday, etc...)
Suggestions?
How about:
var days = ['Sat', 'Sun', 'Mon', 'Tues', 'Wed', 'Thu', 'Fri'];
var selected = days[offset-1];
figured it out, added the following to calculate time in hours:minutes (military time)
var totalMinutes = cellRow * 60;
var hours = Math.floor(totalMinutes / 60);
var minutes = totalMinutes % 60;
$("#displayCoordinates").html("Time: "+hours+":"+minutes+" | Day: "+selected+"</ br> );
精彩评论