Javascript event.offset alternative for final value during CSS scale & transition

huangapple go评论98阅读模式
英文:

Javascript event.offset alternative for final value during CSS scale & transition

问题

我有一个HTML图像,我想要通过编程方式进行平移和缩放。为了提高体验,我添加了CSS过渡以实现平滑效果。

当我点击图像时,我需要确定图像内的鼠标位置。目前,event.offsetX在动画的当前帧中给我提供了鼠标在图像内的位置。

我想要知道鼠标位置,就好像动画已经完成(尽管尚未完成)。

以下是另一种解释问题的方式。我在下面创建了一个示例,在我们单击按钮后,图像会放大。缩放需要5秒钟。在缩放过程中,如果我保持鼠标固定并不断点击,偏移值会随着图像在屏幕上移动而变化,然后在5秒后稳定下来并返回相同的偏移值。如果可能的话,我想要知道动画完成之前的最终偏移值。

  1. <!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->
  2. <!-- 语言:lang-html -->
  3. <button onclick="onButton()">缩放</button>
  4. <br>
  5. <img
  6. id='img'
  7. onclick="onImage(event)"
  8. src="https://picsum.photos/id/237/200"
  9. style="transition: 5s ease"
  10. >
  11. <script>
  12. function onButton() {
  13. const img = document.querySelector('#img')
  14. img.style.scale = 5.0
  15. }
  16. function onImage(event) {
  17. console.log(event.offsetX, event.offsetY)
  18. }
  19. </script>
  20. <!-- 结束代码片段 -->
英文:

I have an HTML image that I want to pan & zoom programatically. In order to improve the experience, I added a CSS transition for smoothness.

When I click on the image, I need to determine the mouse position within the image. Currently, event.offsetX gives me the mouse position within the image at the current frame of the animation.

I would like to know the mouse position as if the animation finished already (even though it hadn't).

Here is another way of explaining the problem. I created below an example where an image zooms in after we click on the button. The zoom takes 5 seconds. During the zoom, if I keep my mouse fixed and keep clicking, the offset value changes as the image moves on the screen and then it stabilizes after 5 seconds and returns the same offset value. I would like to know that final offset value before the animation finishes if possible.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->
<button onclick="onButton()">Zoom</button>
<br>
<img
id='img'
onclick="onImage(event)"
src="https://picsum.photos/id/237/200"
style="transition: 5s ease"
>

  1. &lt;script&gt;
  2. function onButton() {
  3. const img = document.querySelector(&#39;#img&#39;)
  4. img.style.scale = 5.0
  5. }
  6. function onImage(event) {
  7. console.log(event.offsetX, event.offsetY)
  8. }
  9. &lt;/script&gt;

<!-- end snippet -->

答案1

得分: 1

  1. 如果你不介意添加一些额外的HTMLCSS
  2. &lt;!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false --&gt;
  3. &lt;!-- 语言: lang-js --&gt;
  4. function onButton() {
  5. const img = document.querySelector("#img");
  6. img.style.scale = 5.0;
  7. const target = document.querySelector("#target");
  8. target.style.scale = 5.0; // 确保目标的样式与图像的样式匹配
  9. }
  10. function onImage(event) {
  11. console.log(event.offsetX, event.offsetY);
  12. }
  13. &lt;!-- 语言: lang-css --&gt;
  14. #wrapper {
  15. display: inline-block;
  16. position:relative;
  17. font-size:0;
  18. }
  19. #target{
  20. position:absolute;
  21. width:100%;
  22. height:100%;
  23. }
  24. #img {
  25. transition: 5s ease;
  26. z-index:2;
  27. pointer-events:none;
  28. }
  29. &lt;!-- 语言: lang-html --&gt;
  30. &lt;button onclick="onButton()"&gt;放大&lt;/button&gt;
  31. &lt;br&gt;
  32. &lt;div id="wrapper"&gt;
  33. &lt;div id="target" onclick="onImage(event)"&gt;&lt;/div&gt;
  34. &lt;img id="img" src="https://picsum.photos/id/237/200"&gt;
  35. &lt;/div&gt;
  36. &lt;!-- 结束代码片段 --&gt;
  37. 这段代码通过在图像后面放置一个不可见的div来实现放大效果,不使用过渡,并将点击事件传递到该div而不是图像本身。
英文:

If you don't mind adding some additional HTML and CSS:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. function onButton() {
  2. const img = document.querySelector(&quot;#img&quot;);
  3. img.style.scale = 5.0;
  4. const target = document.querySelector(&quot;#target&quot;);
  5. target.style.scale = 5.0;// make sure target&#39;s style matches img&#39;s style
  6. }
  7. function onImage(event) {
  8. console.log(event.offsetX, event.offsetY);
  9. }

<!-- language: lang-css -->

  1. #wrapper {
  2. display: inline-block;
  3. position:relative;
  4. font-size:0;
  5. }
  6. #target{
  7. position:absolute;
  8. width:100%;
  9. height:100%;
  10. }
  11. #img {
  12. transition: 5s ease;
  13. z-index:2;
  14. pointer-events:none;
  15. }

<!-- language: lang-html -->

  1. &lt;button onclick=&quot;onButton()&quot;&gt;Zoom&lt;/button&gt;
  2. &lt;br&gt;
  3. &lt;div id=&quot;wrapper&quot;&gt;
  4. &lt;div id=&quot;target&quot; onclick=&quot;onImage(event)&quot;&gt;&lt;/div&gt;
  5. &lt;img id=&quot;img&quot; src=&quot;https://picsum.photos/id/237/200&quot;&gt;
  6. &lt;/div&gt;

<!-- end snippet -->

This work by zooming an invisible div behind the image without transition, and transfer the click event to that div instead of the image itself

huangapple
  • 本文由 发表于 2023年6月1日 23:41:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76383602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定