document.querySelector(".container").addEventListener('wheel', e => {
e.preventDefault();
let element = document.querySelector(".grid");
let val = getTranslateValues(element);
element.style.transform=`translateX(${(parseInt(val.x) + e.deltaX) }px)`
})
document.querySelector(".container").addEventListener('touchmove', (e) => {
e.preventDefault();
console.log(e);
let element = document.querySelector(".grid");
let val = getTranslateValues(element);
element.style.transform=`translateX(${(parseInt(val.x) + e.deltaX) }px)`
});
/**
* From -> https://zellwk.com/blog/css-translate-values-in-javascript/
* Gets computed translate values
* @param {HTMLElement} element
* @returns {Object}
*/
function getTranslateValues (element) {
const style = window.getComputedStyle(element)
const matrix = style['transform'] || style.webkitTransform || style.mozTransform
// No transform property. Simply return 0 values.
if (matrix === 'none' || typeof matrix === 'undefined') {
return {
x: 0,
y: 0,
z: 0
}
}
// Can either be 2d or 3d transform
const matrixType = matrix.includes('3d') ? '3d' : '2d'
const matrixValues = matrix.match(/matrix.*\((.+)\)/)[1].split(', ')
// 2d matrices have 6 values
// Last 2 values are X and Y.
// 2d matrices does not have Z value.
if (matrixType === '2d') {
return {
x: matrixValues[4],
y: matrixValues[5],
z: 0
}
}
// 3d matrices have 16 values
// The 13th, 14th, and 15th values are X, Y, and Z
if (matrixType === '3d') {
return {
x: matrixValues[12],
y: matrixValues[13],
z: matrixValues[14]
}
}
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.