概要
fullcalendarで現在時刻の所に赤い線を入れる。
1分ごとに動く。
こんなやつ↓
使用環境
Rails v4.1.0
fullcalendar v2.3.0
jquery-rails v3.1.2
ロジック
1、カレンダーはテーブル形式なので全く同じテーブルをもう一つ挿入する。
2、今日の日付またはユーザーの設定などから曜日を取得。
2、取得した曜日のところだけcssでvisibility == visibleを設定して表示する
実際のコード
calendar.js
function setNowBorderLine(calendar){
var elm = $('.fc-today.fc-state-highlight');
if(elm.length == 2) {
var height = $('.fc-time-grid').height();
var border_height = height/1440;
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var per = Math.round((hours * 60 + minutes) * border_height);
var html =('<table id="now_border" style="width: 100%;
line-height:0px;"><tr><td id="yajirushi"
class="fc-axis fc-widget-content"
style="width:31px;">▶</td>');
for (var i = 0; i < 7; i++) {
html += '<td class="week_no' + i + '"'+'> </td>';
}
html += '<td style="border:none;display:none;"> </td></tr></table>';
$('#now_border').remove();
$('.fc-time-grid').prepend(html);
$('#now_border').css('top',per +'px');
$('#now_border').find('td').removeClass('disp-now-border');
var youbi = now.getDay();
var user_setting_youbi = $('#week-start-day').data('week-start-day');
if (user_setting_youbi == 0 ) {
$('td.week_no'+youbi).addClass('disp-now-border');
}else if(user_setting_youbi == 1){
if(youbi == 0){
var getsuyoubi = 6
}else{
var getsuyoubi = youbi - 1
}
$('td.week_no'+getsuyoubi).addClass('disp-now-border');
}else if(user_setting_youbi == 10){
$('td.week_no0').addClass('disp-now-border');
}
$('.disp-now-border').css('visibility', 'visible');
window.setTimeout(function() {
this.setNowBorderLine();
}, 60000);
}
}
$(document).ready(function(){
var calendar = $('#fullcalendarを表示するid名').fullCalendar({});
setNowBorderLine();
});
calendar.css.scss
#now_border{
position: absolute;
z-index: 100;
.disp-now-border{
border-bottom: 1px solid red;
display: block;
position:relative;
}
}
tableのtrには▶︎を追加し、一番左に設置して目印にしました。
tdは曜日によって分けないといけないので、それぞれ違うクラスを付与しないといけない。
上記はユーザーの設定に応じて曜日を変えるようにしています。
最後にSetTimeoutを使って1分ごとに実行するようにすればリアルタイムで線が動きます。
最初に var elm = $('.fc-today.fc-state-highlight');を取得しているのは
カレンダーのPrev、Nextをクリックして他の週を表示したときに
ラインが出ないようにするためです。