【问题标题】:Dynamically positioning scaled html image动态定位缩放的 html 图像
【发布时间】:2012-07-07 22:56:01
【问题描述】:

在 html 页面上放置了一张地图作为背景图像。还有一个透明的图像表示来自地图上某个点的声波,即波的中心。当波浪图像以其完整尺寸放置在地图顶部时,波浪图像的中心位于正确的位置(指向地图上所需的中心点),使用#waveImage {position:absolute;bottom:0;right:0} css 规则。

问题是我需要基于任意算法动态缩放波浪图像,永远不会大于其原始大小。显然,当图像较小时,由于初始 css 定位,中心点从原始中心偏移,朝向右侧和底部,所以我必须移动波浪图像,使波浪中心仍然指向相同地图上的位置。我需要的是从右侧和底部计算正确偏移量的算法。

一些可能有进一步帮助的信息:Wave 图像为 673px x 655px 大,wave 的中心不在图像的中心。

【问题讨论】:

  • 请向我们展示您的尝试。或者,至少,你有什么。你有没有尝试过解决这个问题?您能否在JS Fiddle 或类似网站上向我们展示您正在使用的内容的现场演示(代表SSCCE)?
  • 您可以找到问题的交互式演示here。当您更改新的宽度值时,波形图像会重新缩放。您可以使用相关输入框调整右侧和底部偏移。我所做的只是准备这个演示来了解公式。编辑:我在地图上放了一个黑点,这样它就可以代表静态中心,它的位置不必精确,它只是为了给出一个想法。
  • 查看 CSS 的 zoomtransformtransform-origin 等功能。

标签: html css positioning scale offset


【解决方案1】:

http://jsfiddle.net/k7uGu/6/

HTML:

<div class="map">
    <div class="signal-wrap">
        <img class="signal" src="signal.png">
    </div>
</div>​

CSS

.map {
    background: url(map.png) no-repeat;
    position: relative;
    width:  770px;
    height: 688px;
}

.signal-wrap {
    position: absolute;

    /* find/set the center position on the map,
       using the right bottom corner for positioning
       to avoid "surprise" scrollbars appearing
    */
    right:  28%;
    bottom: 33%;

    /* change these to resize signal.
       you can always use a square shape (width=height),
       even if the image inside is not square
    */
    width:  10%;
    height: 10%;
}

.signal {
    display: block;
    position: absolute;

    /* image width will be equal to parent square's size
       height must not be set to keep original image shape
    */
    width: 100%;

    /* position of the center of the signal
       relative to the parent square
    */
    right:  -23%;
    bottom: -20%;
}

【讨论】:

  • 这是一个非常有创意和鼓舞人心的作品,谢谢。我不明白你是如何发现这些神奇数字的,即正确的:-23%;底部:-20%;当信号大小发生变化时,这似乎可以通过保持中心稳定来达到目的。
  • 它们标记了波浪的中心,我只是调整它们直到看起来不错。当然你可以测量你的原始图像并使用100*(wave_center_x - image_width)100 - 100*(wave_center_x - image_width)
  • 太好了,再次感谢。我只是想知道是否有任何方法不使用这些神奇的数字。我的意思是不使用 wave_center_x 的公式 我最初的尝试是找到通过从右侧和底部“轻推”来自动校正中心但无法弄清楚的公式。
  • 自己的回答都看不懂是不是很可怜?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-28
相关资源
最近更新 更多