MediaWiki:Common.js: Unterschied zwischen den Versionen

Aus Infopedia
Wechseln zu:Navigation, Suche
Zeile 3: Zeile 3:
 
var lastTouchTime = 0;
 
var lastTouchTime = 0;
 
var isTouched = false;
 
var isTouched = false;
 +
var isTouchDevice = false;
 
var timer = null;;
 
var timer = null;;
 
var touchduration = 500;
 
var touchduration = 500;
 +
 +
function addHover() {
 +
  var touch_div = document.getElementsByClassName("touch-dev");
 +
  for(var i = 0; i < touch_div.length; i++)
 +
  {
 +
    touch_div[i].classList.add('hasHover');
 +
  }
 +
}
 +
 +
function removeHover() {
 +
  var touch_div = document.getElementsByClassName("touch-dev");
 +
  for(var i = 0; i < touch_div.length; i++)
 +
  {
 +
    touch_div[i].classList.remove('hasHover');
 +
  }
 +
}
  
 
function mousemove_event() {
 
function mousemove_event() {
   if (isTouched || (new Date() - lastTouchTime < 500)) return;
+
   if (isTouched || (new Date() - lastTouchTime < 500)) return
   document.body.classList.add('hasHover');
+
   addHover();
 +
  isTouchDevice = false;
 
}
 
}
  
 
function touchstart_event() {
 
function touchstart_event() {
 +
  if (!isTouchDevice) removeHover();
 
   isTouched = true;
 
   isTouched = true;
   document.body.classList.remove('hasHover');
+
   isTouchDevice = true;
 
}
 
}
  
Zeile 19: Zeile 38:
 
   isTouched = false;
 
   isTouched = false;
 
   lastTouchTime = new Date();
 
   lastTouchTime = new Date();
  document.body.classList.remove('hasHover');
 
 
}
 
}
  
Zeile 26: Zeile 44:
 
document.addEventListener('mousemove', mousemove_event, true);
 
document.addEventListener('mousemove', mousemove_event, true);
  
mousemove_event();
+
addHover();
  
 
function touchstart_element_event() {
 
function touchstart_element_event() {

Version vom 5. Oktober 2021, 20:24 Uhr

/* Das folgende JavaScript wird für alle Benutzer geladen. */

var lastTouchTime = 0;
var isTouched = false;
var isTouchDevice = false;
var timer = null;;
var touchduration = 500;

function addHover() {
  var touch_div = document.getElementsByClassName("touch-dev");
  for(var i = 0; i < touch_div.length; i++)
  {
    touch_div[i].classList.add('hasHover');
  }
}

function removeHover() {
  var touch_div = document.getElementsByClassName("touch-dev");
  for(var i = 0; i < touch_div.length; i++)
  {
    touch_div[i].classList.remove('hasHover');
  }
}

function mousemove_event() {
  if (isTouched || (new Date() - lastTouchTime < 500)) return
  addHover();
  isTouchDevice = false;
}

function touchstart_event() {
  if (!isTouchDevice) removeHover();
  isTouched = true;
  isTouchDevice = true;
}

function touchend_event() {
  isTouched = false;
  lastTouchTime = new Date();
}

document.addEventListener('touchstart', touchstart_event, true);
document.addEventListener('touchend', touchend_event, true);
document.addEventListener('mousemove', mousemove_event, true);

addHover();

function touchstart_element_event() {
  this.classList.add('touchDevice-hovered');
  if (!timer) {
    timer = setTimeout(onlongtouch.bind(this), touchduration);
  }
}

function touchend_element_event() {
  this.classList.remove('touchDevice-hovered');
  this.classList.remove('touchDevice-longpress')
  cancel_timer();
}

function cancel_timer() {
  if (timer) {
    clearTimeout(timer);
    timer = null;
  }
}

function onlongtouch() { 
  timer = null;
  this.classList.add('touchDevice-longpress');
}

var touch_div = document.getElementsByClassName("touch-dev");
for(var i = 0; i < touch_div.length; i++)
{
  touch_div[i].addEventListener('touchstart', touchstart_element_event, true);

  touch_div[i].addEventListener('touchend', touchend_element_event, true);
  touch_div[i].addEventListener('touchmove', cancel_timer, true);

  touch_div[i].addEventListener('touchcancel', cancel_timer, true);
}