Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can get div position Based on the percentage?

whit this code you can get position a div based on pixel.

$('#div').position();

but how can give div position Based on the percentage?

like image 908
naziiii Avatar asked Jul 17 '13 15:07

naziiii


People also ask

How to use percentage in CSS?

The <percentage> CSS data type represents a percentage value. It is often used to define a size as relative to an element's parent object. Numerous properties can use percentages, such as width , height , margin , padding , and font-size . Note: Only calculated values can be inherited.

How does top CSS work?

The top property affects the vertical position of a positioned element. This property has no effect on non-positioned elements. If position: absolute; or position: fixed; - the top property sets the top edge of an element to a unit above/below the top edge of its nearest positioned ancestor.

What is top HTML?

The top property sets or returns the top position of a positioned element. This property specifies the top position of the element including padding, scrollbar, border and margin. Tip: A positioned element is an element with the position property set to: relative, absolute, or fixed.


1 Answers

Use window size.

var position = $('#div').position();
var percentLeft = position.left/$(window).width() * 100;
var percentTop = position.top/$(window).height() *100;

This code will give you the position percentage for the top and left sides.

like image 158
Jnatalzia Avatar answered Sep 21 '22 10:09

Jnatalzia