Skip to main content
 首页 » 编程设计

javascript之打开当前日期的中心 Vis.js 时间线

2025年05月04日15kenshinobiy

我有一个从 2017 年 1 月到 2018 年 1 月的 VisJS 时间表。时间线以年中三个月的范围为中心打开,但我希望它每次都以当前时间为中心打开。

min: new Date(2017, 1, 5),           // lower limit of visible range 
max: new Date(2018, 1, 11),          // upper limit of visible range 
zoomMin: 1000 * 60 * 60 * 24,        // one day in milliseconds 
zoomMax: 1000 * 60 * 60 * 24*31*3,   // three months in milliseconds 

请您参考如下方法:

你可以试试这样的东西(timeline.setWindow()):

const todayStart = new Date(); 
todayStart.setHours(8, 0, 0, 0); 
const todayEnd = new Date(); 
todayEnd.setHours(18, 0, 0, 0); 
 
console.log(todayStart, ':', todayEnd); 
setTimeout(_ => { 
  this.timeline.setWindow(todayStart, todayEnd, { animation: true }); 
}); 

或更好的 moveTo
  this.timeline.moveTo(new Date());//or 
  this.timeline.moveTo(new Date(), { animation: true });//or 
  this.timeline.moveTo(new Date(), { animation: true }, (props) => { 
    console.log("movedTo", props); 
  });