First, in this way
_block.css('bottom', '4%')
you set the value to bottom , not read it.
To read the value, omit the second argument. Like this:
_block.css('bottom')
Secondly, javascript will return the calculated value in pixels , not in percents.
Third, the use of inline-styles is generally considered bad form.
There is no direct way to get a percentage value. However, this does not mean that it is generally impossible.
The simplest (in terms of code) option, if the value of the bottom is written as you have in inline-styles :
var _menu = $("#menu"); var _block = $("#block"); _menu.click(function () { if (_block[0].style.bottom === '4%') { _block.animate({ 'bottom': '10%' }, 300); } else { _block.animate({ 'bottom': '4%' }, 300); } });
The variant is more complicated, which is able to read the value not only from inline-styles, but also from ordinary ones.
var _menu = $("#menu"); var _block = $("#block"); _menu.click(function () { /* Высота нашего блока */ var bH = parseFloat($('#block').css('bottom')); /* Высота родителя блока, по умолчанию берем высоту окна */ var bParentH = $(window).height(); /* Перебираем всех родителей нашего блока (уже отсортированы по близости) */ _block.parents().each(function() { /* Высота элемента родителя */ var elementH = Math.ceil(parseFloat($(this).css('height'))); /** * Интересуют только те элементы, у которых заданы: * - относительное позиционирование * - высота */ if ($(this).css('position') === 'relative' && elementH > 0) { bParentH = elementH; /* Выходим из цикла */ return false; } }); /* Значение из inline-стилей */ var bInlineHInPercentage = _block[0].style.bottom; /* Автоматически рассчитываемое значение */ var bCalculatedHInPercentage = Math.ceil(100 * bH / bParentH) + '%'; /* Приоритет отдаем значению из inline-стилей, если таковое есть */ var bHInPercentage = bInlineHInPercentage || bCalculatedHInPercentage; if (bHInPercentage === '4%') { _block.animate({ 'bottom': '10%' }, 300); } else { _block.animate({ 'bottom': '4%' }, 300); } });
Option 1: positioning relative to the entire document
Option 1: positioning inside a block
Option 2: positioning relative to the entire document
Option 2: positioning inside a block