我有一個錨點元素環繞圖像,提供常用的鏈接功能以及點擊功能的擴展。如果啓用Javascript,則錨點元素應該佔據整個屏幕,並顯示一個白色背景,並且點擊後圖像應位於中心。目前圖像位於屏幕的左上角。我在「Stackoverflow」中嘗試了一個類似的例子,但它似乎不適用於我的情況。下面是我的代碼:如何在錨點元素內動態調整圖片的中心位置?
<script>
function resize(me) {
$(me).toggleClass('pop_up');
$(me).find('img').toggleClass('resize');
}
</script>
<div>
<a href='http://upload.wikimedia.org/wikipedia/commons/thumb/a/a5/Flower_poster_2.jpg/361px-Flower_poster_2.jpg' onclick='resize(this); return false;'>
<img src='http://upload.wikimedia.org/wikipedia/commons/thumb/a/a5/Flower_poster_2.jpg/361px-Flower_poster_2.jpg'/>
</a>
</div>
這是CSS:
img {
height: 100px;
width: 100px;
}
.resize {
height: auto;
width: auto;
}
.pop_up {
position: fixed;
top: 0;
left: 0;
height: 100%;
width: 100%;
background-color: white;
}
對不起,我想很多人都誤解了我的問題。它在點擊之後將圖像居中,而不是之前。
能不能做到沒有插件?我正在尋找一個CSS解決方案。 – 2012-03-15 13:44:20
無論如何,jQuery的好處是常見的JavaScript用法的一般跨瀏覽器抽象。 – antonjs 2012-03-15 13:49:32
只有css才能完成插件。你將不得不使用表,這是可怕的。 – thiagofm 2012-03-15 18:16:12