![](/img/trans.png)
[英]how to replace div2 with other div1 at exact same position of div1 in js or jquery
[英]Converting an <area> into a <div> with the exact same position and shape using JS/CSS
我正在嘗試將這個類型為circle的圖像映射轉換為具有特定的頂部和左側坐標以及基於這些坐標的適當大小的div(300,115,10)
<img src="http://localhost//images/baby.png" alt="Planets" usemap="#MyMap">
<map name="MyMap">
<area alt="Venus" href="venus.htm" coords="300,115,10" shape="circle">
</map>
現在可以從這些坐標中提取頂部,左側,寬度和高度,並構造一個類似於圖像地圖的圓形div了嗎? 任何javascript / css代碼都將有所幫助。
檢查一下: >>小提琴<<
腳步:
創建一個新的容器div,其大小和位置與映射的圖像相同。
var $img = $(img); var $imgmap = $("<div class='imgmap'></div>"); $img.after($imgmap); var imgheight = $img.height(); var imgwidth = $img.width(); var imgPosition = $img.position(); $imgmap.css( { top:imgPosition.top+"px", left:imgPosition.left+"px", height:imgheight+"px", width:imgwidth+"px" });
在地圖內找到圖片的地圖名稱和圓圈
var mapName = $img.attr("usemap").replace("#",""); var circles = $("map[name='"+mapName+"'] area[shape='circle']");
遍歷所有圈子
circles.each(function(index,circle){ var $circle = $(circle); var attrs = $circle.attr("coords").split(",");//get coords attribute and turn it in to an arrat var alt = $circle.attr("alt"); // get alt of the area var $newa = $("<a class='mapcircle "+alt+"' href='"+$circle.attr("href")+"' alt='"+alt+"'></a>"); //create a new anchor $imgmap.append($newa); //append that to previously created container //apply position and size var size = (attrs[2]*2)+'px' $newa.css( { left:attrs[0]+'px', top:attrs[1]+'px', width:size, height:size }); });
CSS:
容器css,絕對定位,因此我們可以使用jquery的positon()函數並使用該值。 注意:如果圖像移動,就像改變位置()返回的值一樣,則必須重新定位div。 解決方案可能是,將包括圖像的所有東西相對定位或包裝在另一個容器中,然后用它替換圖像。
.imgmap{ position:absolute; z-index:10; box-sizing:border-box; margin:0; padding:0; }
行星! 很簡單,但是:默認顏色是綠色,半徑50%使它們變圓,新類(由區域的alt屬性賦予)用於單獨的着色。
a.mapcircle{ display:block; position:absolute; background-color:green; border-radius:50%; } .mapcircle.Venus{ background-color:yellow; } .mapcircle.Earth{ background-color:red; opacity:0.5; }
試試這個尺寸...
演示: http : //jsfiddle.net/mTM4q/2/
<div>
<img src="http://placekitten.com/500/400" />
<a class="planet venus" href="#venus"></a>
<a class="planet jupiter" href="#jupiter"></a>
</div>
div {
position: relative;
}
div > * {
position: absolute;
}
.planet {
border-radius: 50%;
}
.venus {
width: 100px;
height: 100px;
left: 300px;
top: 115px;
background-color: red;
}
.jupiter {
width: 250px;
height: 250px;
top: 100px;
left: 50px;
background-color: blue;
}
我所做的是將每個.planet
的坐標放入CSS中。 這與top
和left
CSS值相對應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.