Impress.js 和使用 HTML5 数据属性代替 CSS 转换

发布于 2024-12-27 19:22:08 字数 291 浏览 4 评论 0原文

我一直在使用 Impress.js 制作演示文稿,并正在考虑对其进行调整在 iPad 上使用。但我担心 Impress 如何将变换应用于每张幻灯片。不只是使用 CSS,而是将转换作为数据属性放入 HTML 中,然后使用 Javascript 进行应用。

我觉得这是一种过于复杂和困难的做事方式。不过,我在处理数据属性方面没有太多经验,所以我想就此获得一些意见。正常编写 CSS 会更好,还是使用数据属性的好处大于麻烦?

I've been working on a presentation using Impress.js, and was thinking of adapting it for use on iPads. But I'm concerned about how Impress applies transforms to each slide. Instead of just using CSS, transforms are put in the HTML as data attributes, then applied with Javascript.

This strikes me as an overly complicated and difficult way of doing things. I don't have a lot of experience working with data attributes, though, so I wanted to get some opinions on this. Would it be better to just write CSS normally, or do the benefits of using data attributes outweigh the hassle?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事未完 2025-01-03 19:22:08

原因很简单:如果您设置了 CSS 转换(通过 javascript 或 CSS),当您尝试获取它时,它会返回一个矩阵转换。假设你做了一些简单的事情,例如...

element.style.webkitTransform = "translate3d(10px,0,0)";
var transform = element.style.webkitTransform;

transform 不会是 translate3d(10px,0,0),它将是 matrix3d(10px,0, 0,[...])。要获取这些值,您需要解析矩阵,例如...

element.style.webkitTransform = "translate3d(10px,0,0)";
var matrix = new WebKitCSSMatrix(element.style.webkitTransform); //get the matrix
var x = matrix.m41 //get the value of x in the matrix
x += 1500 //modify x
element.style.webkitTransform = matrix.toString //Apply the new transformation

这与 data-attr 的代码相同:

  element.style.webkitTransform = "translate3d(10px,0,0)";
  x = element.dataset.x
  element.style.webkitTransform = "translate3d(" + x + ",0,0)"

您可以在此处使用 3D CSS 矩阵:

http://jsfiddle.net/F8xLv/

The reason is simple: if you set a CSS transformation—either through javascript or CSS—when you try to get it it returns a matrix transformation. So say you do something simple like...

element.style.webkitTransform = "translate3d(10px,0,0)";
var transform = element.style.webkitTransform;

transform won't be translate3d(10px,0,0), it will be matrix3d(10px,0,0,[...]). To get these values you'd need to parse the matrix such as...

element.style.webkitTransform = "translate3d(10px,0,0)";
var matrix = new WebKitCSSMatrix(element.style.webkitTransform); //get the matrix
var x = matrix.m41 //get the value of x in the matrix
x += 1500 //modify x
element.style.webkitTransform = matrix.toString //Apply the new transformation

This is the same code with data-attr:

  element.style.webkitTransform = "translate3d(10px,0,0)";
  x = element.dataset.x
  element.style.webkitTransform = "translate3d(" + x + ",0,0)"

You can play around with the 3D CSS Matrix here:

http://jsfiddle.net/F8xLv/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文