Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Created by James on 2017-02-25.
  3.  */
  4. window.addEventListener("load", init);
  5.  
  6. function init() {
  7.     // Event Listeners
  8.  
  9.     // box1 event Listener
  10.     var box1 = document.getElementById("box1");
  11.     box1.addEventListener("click", box1_color);
  12.  
  13.     // box 3
  14.     var box3 = document.getElementById("box3");
  15.     box3.addEventListener("click", box3_click);
  16.  
  17.     // box 4
  18.     var box4 = document.getElementById("box4");
  19.     box4.addEventListener("click", box4_click);
  20.  
  21.     // box 2
  22.     var box2 = document.getElementById("box2");
  23.     box2.addEventListener("dragstart", box2_dragstart);
  24.     box2.setAttribute("draggable", true);
  25.  
  26.     // box 5
  27.     var box5 = document.getElementById("box5");
  28.     box5.addEventListener("drop", box5_drop);
  29.     box5.addEventListener("drop", box5_dragover);
  30. }
  31.  
  32.     // Global Variables
  33.     var last_color = 0 ; // bit 2 is red, bit 1 is green, bit 0 blue
  34.     var counter = 0 ;  // counter variable
  35.     var bcolor = 0 ;  // page bg color: 0 for black 1 is white
  36.  
  37.     // Writing box1_color(evt)
  38.    function box1_color()
  39.    {
  40.         if(last_color == 8)
  41.         {
  42.             last_color = 0 ;
  43.         }
  44.  
  45.        last_color++;
  46.  
  47.        var color_to_set = "#";
  48.        color_to_set += (last_color & 4) ? '0' : 'F';
  49.        color_to_set += (last_color & 2) ? '0' : 'F';
  50.        color_to_set += (last_color & 1) ? '0' : 'F';
  51.  
  52.        var bcolor_to_set = "#";
  53.        bcolor_to_set += (last_color & 4) ? 'F' : '0';
  54.        bcolor_to_set += (last_color & 2) ? 'F' : '0';
  55.        bcolor_to_set += (last_color & 1) ? 'F' : '0';
  56.  
  57.        this.style.backgroundColor = bcolor_to_set;
  58.        this.style.color = color_to_set;
  59.  
  60.        last_color++;
  61.    }
  62.  
  63.    function box3_click()
  64.    {
  65.        this.innerHTML = "<p>Count = " + counter + ".</p>";
  66.        counter++;
  67.    }
  68.  
  69.    // Writing box4_click() @@@ <<<
  70.    function box4_click()
  71.    {
  72.         if(bcolor == 0)
  73.         {
  74.             document.documentElement.style.backgroundColor = "#000";
  75.             bcolor = 1;
  76.         }
  77.         else if(bcolor == 1)
  78.         {
  79.             document.documentElement.style.backgroundColor = "#FFF";
  80.             bcolor = 0 ;
  81.         }
  82.    }
  83.  
  84.  
  85. function box2_dragstart(evt)
  86. {
  87.     evt.dataTransfer.setData("text/plain",counter);
  88. }
  89.  
  90. function box5_drop(evt)
  91. {
  92.     evt.preventDefault(); // turns off default event processing for the target element
  93.     var data = evt.dataTransfer.getData("text/plain");
  94.     this.innerHTML = "counter =" + data ;
  95. }
  96.  
  97. function box5_dragover(evt)
  98. {
  99.     evt.preventDefault();
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement