2012-07-07 119 views
0

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

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

一些信息可能会有帮助:波形图像是673px x 655px大,波浪的中心不在图像的中心。

+1

请告诉我们你已经尝试了什么。或者,至少,你有什么。你有没有试图解决这个问题?你能否向我们展示一下你在[JS Fiddle](http://jsfiddle.net/)或类似工作中的实况(代表性,[SSCCE](http://sscce.org/))演示? – 2012-07-07 22:58:59

+0

你可以在这里找到一个问题的交互式演示(https://egemadra.net/demo1/)。当您更改新的宽度值时,波形图像将重新缩放。您可以使用相关输入框来调整右侧和底部偏移量。我所尝试的是准备这个演示来了解这个公式。编辑:我在地图上放了一个黑点,以便它可以代表静态中心,它的位置不一定非常精确,它只是为了给出一个想法。 – egemadra 2012-07-07 23:32:26

+0

请参阅CSS的“缩放”,“变换”,“变换原点”等功能。 – biziclop 2012-07-08 07:47:00

回答

2

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%; 
} 
+0

这是一个非常有创意和鼓舞人心的工作,谢谢。我没有得到的是你如何发现这些神奇的数字,即正确的:-23%;底部:-20%;当信号在尺寸上发生变化时,通过保持中心稳定,似乎可以做到这一点。 – egemadra 2012-07-10 06:06:52

+0

它们标记着波浪的中心,我只是调整它们直到它看起来不错。当然,你可以测量你的原始图像,并使用'100 *(wave_center_x - image_width)'或'100 - 100 *(wave_center_x - image_width)'。 – biziclop 2012-07-10 06:10:53

+0

例如:http://jsfiddle.net/G3Fuj/ :) – biziclop 2012-07-10 06:18:19