在完整日历中使用随机颜色生成器使BackgroundColor等于边框颜色

时间:2017-07-12 03:26:52

标签: javascript html css twitter-bootstrap-3 fullcalendar

我正在使用完整日历为我的客户创建一个很容易查看的计划。现在我尝试让每个事件都有不同的颜色,因为我使用代码在每个页面加载时生成随机颜色,但现在的问题是边框颜色和背景颜色不相等。它们不相等,因为我还添加了边框颜色的随机颜色。但是当我尝试将getRandomColor函数分配给变量并将变量放在每个事件的每个边框和背景颜色中时,它确实会为事件输出相同的边框和背景颜色,但它将被应用对于所有的事件。所以我的目标不符合。

当存在随机颜色时,如何使背景颜色等于边框颜色?

JSFIDDLE:https://jsfiddle.net/aice09/w1pxfzcm/2/



$(document).ready(function() {
    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth() + 1; //January is 0!

    var yyyy = today.getFullYear();
    if (dd < 10) {
        dd = '0' + dd;
    }
    if (mm < 10) {
        mm = '0' + mm;
    }
    var today = yyyy + '-' + mm + '-' + dd;

    function getRandomColor() {
        var letters = '0123456789ABCDEF';
        var color = '#';
        for (var i = 0; i < 6; i++) {
            color += letters[Math.floor(Math.random() * 16)];
        }
        return color;
    }

    var genColor =getRandomColor();
    
    $('#calendar').fullCalendar({


        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,agendaWeek,agendaDay,listWeek'
        },
        defaultDate: today,
        navLinks: true, // can click day/week names to navigate views
        editable: false,
        eventLimit: true, // allow "more" link when too many events
        events: [{
            title: 'Philippine Seven Corporation',
            start: '2017-05-01',
            backgroundColor: getRandomColor()
        }, {
            title: 'Long Event',
            start: '2017-05-07',
            end: '2017-05-10',
            backgroundColor: getRandomColor(),
            borderColor: getRandomColor()
        }, {
            id: 999,
            title: 'Repeating Event',
            start: '2017-05-09T16:00:00',
            backgroundColor: getRandomColor()
        }, {
            id: 999,
            title: 'Repeating Event',
            start: '2017-05-16T16:00:00',
            backgroundColor: getRandomColor(),
            borderColor: getRandomColor()
        }, {
            title: 'Conference',
            start: '2017-05-11',
            end: '2017-05-13',
            backgroundColor: genColor,
            borderColor: genColor
        }, {
            title: 'Meeting',
            start: '2017-05-12T10:30:00',
            end: '2017-05-12T12:30:00',
            backgroundColor: genColor,
            borderColor: genColor
        }, {
            title: 'Lunch',
            start: '2017-05-12T12:00:00',
            backgroundColor: genColor,
            borderColor: genColor
        }, {
            title: 'Meeting',
            start: '2017-05-12T14:30:00',
            backgroundColor: getRandomColor(),
            borderColor: getRandomColor()
        }, {
            title: 'Happy Hour',
            start: '2017-05-12T17:30:00',
            backgroundColor: getRandomColor(),
            borderColor: getRandomColor()
        }, {
            title: 'Dinner',
            start: '2017-05-12T20:00:00',
            backgroundColor: getRandomColor(),
            borderColor: getRandomColor()
        }, {
            title: 'Birthday Party',
            start: '2017-05-13T07:00:00',
            backgroundColor: getRandomColor(),
            borderColor: getRandomColor()
        }, {
            title: 'Click for Google',
            url: 'http://google.com/',
            start: '2017-05-28',
            backgroundColor: getRandomColor(),
            borderColor: getRandomColor()
        }],
        eventClick: function(event) {
            if (event.title) {
                alert(event.title);
            }
        }
    });

});
&#13;
#calendar {
    width: 100%;
}
&#13;
   <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
    <!--Full Calendar-->
    <script type="text/javascript" src="https://code.jquery.com/ui/1.11.2/jquery-ui.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.2.6/fullcalendar.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.2.6/fullcalendar.min.css">
    <!--Bootstrap 3.3.7-->
    <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div id='calendar'></div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:3)

你可以在getRandomColor中有一个参数来让它返回先前计算的颜色。

$(document).ready(function() {
  var today = new Date();
  var dd = today.getDate();
  var mm = today.getMonth() + 1; //January is 0!

  var yyyy = today.getFullYear();
  if (dd < 10) {
    dd = '0' + dd;
  }
  if (mm < 10) {
    mm = '0' + mm;
  }
  var today = yyyy + '-' + mm + '-' + dd;
  var prevColor;

  function getRandomColor(usePrev) {
    if (usePrev && prevColor)
      return prevColor;

    var letters = '0123456789ABCDEF';
    var color = '#';
    for (var i = 0; i < 6; i++) {
      color += letters[Math.floor(Math.random() * 16)];
    }
    prevColor = color;
    return color;
  }

  $('#calendar').fullCalendar({


    header: {
      left: 'prev,next today',
      center: 'title',
      right: 'month,agendaWeek,agendaDay,listWeek'
    },
    defaultDate: today,
    navLinks: true, // can click day/week names to navigate views
    editable: false,
    eventLimit: true, // allow "more" link when too many events
    events: [{
      title: 'Philippine Seven Corporation',
      start: '2017-05-01',
      backgroundColor: getRandomColor(),
      borderColor: getRandomColor(true)
    }, {
      title: 'Long Event',
      start: '2017-05-07',
      end: '2017-05-10',
      backgroundColor: getRandomColor(),
      borderColor: getRandomColor(true)
    }, {
      id: 999,
      title: 'Repeating Event',
      start: '2017-05-09T16:00:00',
      backgroundColor: getRandomColor(),
      borderColor: getRandomColor(true)
    }, {
      id: 999,
      title: 'Repeating Event',
      start: '2017-05-16T16:00:00',
      backgroundColor: getRandomColor(),
      borderColor: getRandomColor(true)
    }, {
      title: 'Conference',
      start: '2017-05-11',
      end: '2017-05-13',
      backgroundColor: getRandomColor(),
      borderColor: getRandomColor(true)
    }, {
      title: 'Meeting',
      start: '2017-05-12T10:30:00',
      end: '2017-05-12T12:30:00',
      backgroundColor: getRandomColor(),
      borderColor: getRandomColor(true)
    }, {
      title: 'Lunch',
      start: '2017-05-12T12:00:00',
      backgroundColor: getRandomColor(),
      borderColor: getRandomColor(true)
    }, {
      title: 'Meeting',
      start: '2017-05-12T14:30:00',
      backgroundColor: getRandomColor(),
      borderColor: getRandomColor(true)
    }, {
      title: 'Happy Hour',
      start: '2017-05-12T17:30:00',
      backgroundColor: getRandomColor(),
      borderColor: getRandomColor(true)
    }, {
      title: 'Dinner',
      start: '2017-05-12T20:00:00',
      backgroundColor: getRandomColor(),
      borderColor: getRandomColor(true)
    }, {
      title: 'Birthday Party',
      start: '2017-05-13T07:00:00',
      backgroundColor: getRandomColor(),
      borderColor: getRandomColor(true)
    }, {
      title: 'Click for Google',
      url: 'http://google.com/',
      start: '2017-05-28',
      backgroundColor: getRandomColor(),
      borderColor: getRandomColor(true)
    }],
    eventClick: function(event) {
      if (event.title) {
        alert(event.title);
      }
    }
  });

});
#calendar {
  width: 100%;
}
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<!--Full Calendar-->
<script type="text/javascript" src="https://code.jquery.com/ui/1.11.2/jquery-ui.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.2.6/fullcalendar.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.2.6/fullcalendar.min.css">
<!--Bootstrap 3.3.7-->
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div id='calendar'></div>

答案 1 :(得分:0)

这里的问题与你如何为每个事件调用你的函数两次有关。查看“events”数组中的每个对象,尤其是属性backgroundColor和borderColor。你在每个对象中调用你的函数两次。

{
        title: 'Long Event',
        start: '2017-05-07',
        end: '2017-05-10',
        backgroundColor: getRandomColor(), //<-- right here
        borderColor: getRandomColor() , // <-- calling  again, new color!
    }, 

在我的脑海中,一个可能的解决方案是创建一个生成事件对象的函数。在该事件生成器中调用函数'getRandomColor'或将颜色作为参数传递。