CKTIS / JavaScript
`HTMLElement.style`: transforms and touch gestures
Use `style.transform` to change an element’s visual geometry and `style.touchAction` to control which panning and zooming gestures the browser may handle. Both write inline CSS declarations.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
element.style.transform = "translateX(24px) rotate(10deg)";
element.style.touchAction = "pan-y";
element.style.getPropertyValue("touch-action");
element.style.setProperty("touch-action", "pan-y");
element.style.transform = "";
element.style.removeProperty("touch-action");
getComputedStyle(element).transform;
getComputedStyle(element).touchAction;HTMLElement.style.transform/touchAction names two separate properties on an HTMLElement instance’s style object; the slash is not JavaScript syntax. element.style exposes a live, mutable CSSOM object backed by the style attribute. Assigning to element.style.transform or element.style.touchAction changes only that declaration. With getPropertyValue() and setProperty(), use the CSS names transform and touch-action.
Reading element.style gives "" for a supported property with no inline declaration, even if a stylesheet supplies a value. To inspect a resolved value for an element on the page, use getComputedStyle(element). Assigning "" or calling removeProperty() with the CSS name removes the inline declaration; it does not set the CSS value none.
transform takes none or a space-separated list of functions such as translateX(24px) rotate(10deg). Order matters. A transform changes how the element appears without moving other elements in normal flow. For a transformable CSS box, a transform value other than none creates a stacking context and makes the element a containing block for descendants with position: absolute or position: fixed. An ordinary non-replaced span with display: inline can store the inline transform declaration, but that declaration does not create a stacking context or a containing block on the span.
touch-action does not implement dragging. For example, auto lets the browser handle panning and zooming as usual, while pan-y pinch-zoom permits single-finger vertical panning and pinch zoom, leaving the app free to implement horizontal dragging. At gesture start, the browser intersects the gestures allowed by the touched element and the relevant ancestors; mid-gesture changes do not affect that gesture. If the browser takes over panning or zooming, handle pointercancel to clear drag state. Avoid applying touch-action: none broadly: it can block zoom that people with low vision rely on.
These are web platform APIs, not ECMAScript 2024 features. Support depends on the browser and value. CSS.supports("touch-action", "pan-y pinch-zoom") checks declaration support, but does not replace testing gestures on a touch device.
Examples
<!doctype html>
<meta charset="utf-8">
<title>Inline transform and touch-action</title>
<script>
if (typeof CSS === "undefined" || typeof CSS.supports !== "function") {
console.log("CSS.supports() is not available.");
} else if (CSS.supports("transform", "translateX(24px)") && CSS.supports("touch-action", "pan-y")) {
const element = document.createElement("div");
const style = element.style;
style.transform = "translateX(24px)";
style.touchAction = "pan-y";
console.log(JSON.stringify([style.transform, style.getPropertyValue("touch-action")]));
style.transform = "";
style.removeProperty("touch-action");
console.log(JSON.stringify([style.transform, style.touchAction]));
} else {
console.log("Required CSS values are not supported.");
}
</script>Result
["translateX(24px)","pan-y"] ["",""]
Save the example as an HTML file, open it in a browser, and check the developer console. It shows inline declaration writes and removal on a detached element; it does not test rendering or gestures.
Related lessons
- JavaScript · Lesson 35 · Advanced · Version: 4