2010-08-27 91 views
0

有什麼辦法來位置相關 您HTML元素之一拉伸你的頁面的背景定位元素 - CSS-JQuery的

你已經捉襟見肘的背景下,

現在要寫明旁邊的標誌的東西。

考慮的決議和瀏覽器

CSS & JQuery的

如果您知道任何戰略或任何好的文章/教程

我會很高興收到你

感謝聽到

+1

你是如何拉伸背景的?請顯示一些代碼。 – 2010-08-27 08:12:42

+0

是的, 我拉伸了圖像並使其成爲全屏幕背景。 現在我想將元素放在我們在背景中看到的東西旁邊,例如徽標。 該代碼現在不可用。 – Sypress 2010-08-27 08:40:32

回答

1

您可以計算窗口中的座標以匹配拉伸的座標。因此,如果背景圖像從座標(0,0)一直延伸到(window.screen.width,window.screen.height),圖像大小可以說(800,600),並且您的徽標位於圖像座標(44,100),和你想知道窗口的座標匹配圖像的一個...

function place(div, image_x, image_y, image_w, image_h) 
{ 
    //Calculate the window coordinates that match the image coordinates 
    var window_x = Math.Floor((window.screen.width/image_w)*image_x); 
    var window_y = Math.Floor((window.screen.height/image_h)*image_y); 

    //Make divs position absolute 
    $("#"+div).css("position", "absolute"); 

    //Set the left in the window coords that match the images coords 
    $("#"+div).css("left", window_x+"px"); 

    //Set the top to match the images coords 
    $("#"+div).css("top", window_y+"px"); 
} 

你會使用這樣的功能:

place("logo_text_div", 44, 100, 800, 600); 

您可以修改代碼以使用Ceil,您可以將協調轉移到右側。或者你可以用其他方式將window_x和window_y舍入,這樣你就可以得到你想要的預失真。

0

window.screen.widthwindow.screen.height可能對您有所幫助。