css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <section>
  <h1>Tester les actions sur les images</h1>
  <ul>
    <li>Action en cours : <span id="action">None</span></li>
  </ul>
</section>
<section>
  <div>
    <div class="canvas">
      <img class="content" src="http://img15.deviantart.net/9bd1/i/2016/019/3/0/life_is_strange__together__by_sakimichan-d9okm1i.jpg" alt="" />
    </div>
  </div>
  <div>
    <div class="canvas">
      <img class="content" src="http://img15.deviantart.net/9bd1/i/2016/019/3/0/life_is_strange__together__by_sakimichan-d9okm1i.jpg" alt="" />
    </div>
  </div>
  <div>
    <div class="canvas">
      <img class="content" src="http://img15.deviantart.net/9bd1/i/2016/019/3/0/life_is_strange__together__by_sakimichan-d9okm1i.jpg" alt="" />
    </div>
  </div>
  <div>
    <div class="canvas">
      <img class="content" src="http://img15.deviantart.net/9bd1/i/2016/019/3/0/life_is_strange__together__by_sakimichan-d9okm1i.jpg" alt="" />
    </div>
  </div>
</section>
            
          
!
            
              /** Le CSS ici n'est pas interessant pour la compréhension de cet exemple. **/

/** Chrome : 
- gère la souris avec Click, StartHover, EndHover
- gère le tactile avec Tap, StartSwap, EndSwap **/

/** Firefox, Edge/IE, Safari : 
- gère la souris avec Click, StartHover, EndHover
- gère le tactile avec Click, StartHover, EndHover 

(touchstart, touchmove et touchend sont simulé par des évènements de clique, pour réellement faire marcher ce script comme Chrome, il faut : 
- Edge : taper dans la barre d'adresse « about:flags » et mettre le tactile en marche.
- FF : taper dans la barre d'adresse « about:config » et mettre `dom.w3c_touch_events.enabled` à 2)
**/

/** Android et iOS : 
- gère le tactile avec Tap, StartSwap, EndSwap
**/

/** En associant donc les mêmes actions à Click/Tap et Hover/Swap vous serez sur d'avoir un comportement identique Cross-Browser **/

/** -- Ne fait pas partie des 4 examples -- **/
.canvas {
  cursor: pointer;
  transition: transform 0.2s ease;
}
.canvas.hover {
  transform: scale(1.1);
}
.canvas.action {
  transform: rotate(180deg);
}
.canvas.action.hover {
  transform: scale(1.1) rotate(180deg);
}
#action {
  color: green;
}





/** -- Not part of explaination after this line -- **/
.canvas {
	position: relative;
	width: 100%;
  overflow: hidden;
}
.canvas:before {
	content: "";
	display: block;
	padding-top: 100%;
}
.content {
	position: absolute;
	top: 0;
	height: 100%;
}

section {
  margin: 0 auto;
  max-width: 1070px;
}
div:not(.canvas) {
  width: calc(25% - 8px);
  float: left;
  padding: 2px;
  border: 1px solid #000;
  margin: 4px;
  box-sizing: border-box;
}
            
          
!
            
              var action = document.getElementById("action");

function CTHS(elements, eventListeners) {
  [].forEach.call(elements, function(item) {
    var tapFlag = false,
      clickFlag = false,
      hoverFlag = false,
      swapFlag = false,
      outFlag = false;

    item.addEventListener("click", function(e) {
      if (clickFlag) {
        clickFlag = false;
        return false;
      }
      if (eventListeners.click) {
        eventListeners.click(item, e);
      }
    });
    item.addEventListener("mouseenter", function(e) {
      if (hoverFlag) {
        hoverFlag = false;
        return false;
      }
      outFlag = true;
      if (eventListeners.startHover) {
        eventListeners.startHover(item, e);
      }
    });
    item.addEventListener("mouseout", function(e) {
      if (outFlag) {
        if (eventListeners.endHover) {
          eventListeners.endHover(item, e);
        }
        outFlag = false;
      }
    });
    item.addEventListener("touchend", function(e) {
      if (tapFlag) {
        if (eventListeners.tap) {
          eventListeners.tap(item, e);
        }
      } else {
        if (eventListeners.endSwap) {
          eventListeners.endSwap(item, e);
        }
      }
    });
    item.addEventListener("touchstart", function(e) {
      tapFlag = true;
      clickFlag = true;
      hoverFlag = true;
      swapFlag = true;
    });
    item.addEventListener("touchmove", function(e) {
      tapFlag = false;
      if (swapFlag) {
        if (eventListeners.startSwap) {
          eventListeners.startSwap(item, e);
        }
        swapFlag = false;
      }
    });
  });
}

CTHS(document.getElementsByClassName("canvas"), {
  click: function (element) {
    action.textContent = "Click";
    console.log("Click");
    element.classList.toggle("action");
  },
  startHover: function (element) {
    action.textContent = "StartHover";
    console.log("StartHover");
    element.classList.add("hover");
  },
  endHover: function (element) {
    action.textContent = "EndHover";
    console.log("EndHover");
    element.classList.remove("hover");
  },
  tap: function (element) {
    action.textContent = "Tap";
    console.log("Tap");
    element.classList.toggle("action");
  },
  startSwap: function (element) {
    action.textContent = "StartSwap";
    console.log("StartSwap");
    element.classList.add("hover");
  },
  endSwap: function (element) {
    action.textContent = "EndSwap";
    console.log("EndSwap");
    element.classList.remove("hover");
  }
});
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console