我使用1年视图的计划程序。所以我可以从1月1日到12月31日每天通过水平滚动查看。FullCalendar计划程序滚动到当前日期
小问题是水平滚动总是在初始位置(一直左),所以它总是显示1月1日。有没有办法让它滚动到当前日期或月初始加载?
我正在寻找通过查找当前日期和ScrollLeft
到元素的jQuery滚动。但看起来标题与滚动容器是分开的,所以不知道如何工作。
我使用1年视图的计划程序。所以我可以从1月1日到12月31日每天通过水平滚动查看。FullCalendar计划程序滚动到当前日期
小问题是水平滚动总是在初始位置(一直左),所以它总是显示1月1日。有没有办法让它滚动到当前日期或月初始加载?
我正在寻找通过查找当前日期和ScrollLeft
到元素的jQuery滚动。但看起来标题与滚动容器是分开的,所以不知道如何工作。
对于2.6.1
eventAfterAllRender: function (view) {
var viewStartDate;
if (view.name == 'yearView') {
viewStartDate = new Date(new Date().getFullYear(), 0, 1);
}
else if (view.name == 'twoMonthView' || view.name == 'oneMonthView') {
viewStartDate = new Date(new Date().getFullYear(), new Date().getMonth(), 1);
}
if (view.name == 'oneWeekView') {
$('.fc-body .fc-time-area .fc-scrollpane').children('div').scrollLeft(4 * 12 * (moment().weekday() - 1) * view.options.slotWidth);
}
else {
var dateDiff = (new Date().setHours(0, 0, 0, 0) - viewStartDate)/(1000 * 60 * 60 * 24);
$('.fc-body .fc-time-area .fc-scrollpane').children('div').scrollLeft(Math.round(dateDiff) * view.options.slotWidth);
}
}
版这是在版本2.6.1
var dateDiff = (new Date().setHours(0, 0, 0, 0) - viewStartDate)/(1000 * 60 * 60 * 24);
$('.fc-body .fc-time-area .fc-scrollpane').children('div').scrollLeft(Math.round(dateDiff) * view.options.slotWidth);
在版本3.0+主要部分的HTML的结构是不同的。要滚动的元素已更改。
$('.fc-body .fc-time-area .fc-scroller-clip .fc-scroller').scrollLeft(Math.round(dateDiff) * view.options.slotWidth)
大概可以减少它归结为
$('.fc-scroller').scrollLeft(Math.round(dateDiff) * view.options.slotWidth)
这里没有任何事情发生,我的视图名称是timelineYear,所以我改变了。使用版本3.1.0。我可以看到,如果我手动更改viewStartDate值,则dateDiff值会更改。但似乎最后一行没有找到任何东西 – fr0sty
@ fr0sty你安装了jQuery和Moment.js?我使用的是2.6.1版本,不确定结构是否改变。但'.fc-scrollpane'里的'div'是滚动的。单元格的大小是固定的'view.options.slotWidth'就是'80px'。我只计算从开始(1月1日)到今天的天数,并按宽度(即44天* 80px = 3520px)倍数并向左滚动该像素量。 – Jack
是的,但它看起来像他们改变了3.1.0版本中的所有类。无法找到正确的类以使用scrollLeft。 – fr0sty
我遇到了同样的问题,就在昨天,发现如下解决方案(与fullCalendar 2.6.1工作):
// Scroll the calendar to a specific event
scrollToEvent: function(event) {
// Get the "scroller" divs for header and content
var headerScroller = $('.fc-time-area.fc-widget-header > .fc-scrollpane > div');
var contentScroller = $('.fc-time-area.fc-widget-content > .fc-scrollpane > div');
// Get the destination date
// For some reason event.start.format('YYYY-MM-DDTHH:mm:ss') will be formatted
// as 'YYYY-MM-DDAHH:mm:ss', hence the "replace" hack.
// Maybe I have to dig more into moment js...
var dateSelector = 'th[data-date="' + event.start.format('YYYY-MM-DDTHH:mm:ss').replace('A', 'T') + '"]';
var date = $(dateSelector).last()[0];
// Scroll to date
headerScroller.scrollLeft(date.offsetLeft);
contentScroller.scrollLeft(date.offsetLeft);
// To scroll vertically to a specific resource (if any)...
// Get the destination resource
var resourceSelector = 'tr[data-resource-id="' + event.resourceId + '"]';
var resource = $(resourceSelector).last()[0];
// Scroll to resource
contentScroller.scrollTop(resource.offsetTop);
}
我呼吁来自FullCalendar的“eventAfterAllRender”功能上述功能,使用标志仅检查第一个渲染。不知道是否有一个更好的办法...
简单的代码只是滚动到日期:
scrollToDate: function(date) {
// Get the "scroller" (no need to distinguish between header and content so get both)
var scroller = $('.fc-time-area > .fc-scrollpane > div');
// Get the destination date
var selector = 'th[data-date="' + date.format('YYYY-MM-DDTHH:mm:ss') + '"]';
var date = $(selector).last()[0];
// Scroll to date
scroller.scrollLeft(date.offsetLeft);
}
希望这有助于(这是我对堆栈溢出的第一篇文章)。
不能让你的例子工作,简单的代码没有任何反应。即使编辑了一下。 eventAfterAllRender: function(view){ var scroller = $('.fc-time-area > .fc-scrollpane > div'); // Get the destination date var selector = 'th[data-date="2017-02-12"]'; var date = $(selector).last()[0]; // Scroll to date scroller.scrollLeft(date.offsetLeft); }
–
fr0sty
你有没有解决这个问题? – fr0sty
@ fr0sty是的,我只是写了一些计算来获得偏移量并设置它。让我发布它。 – Jack
@ fr0sty我发布了答案。看起来像别人找到了另一种解决方案,所以你也可以尝试。 – Jack