1
I have a small jQuery script for when loading the page, a calculation is made based on the height of a div
and then add a margin for the result obtained.
The problem is you’re not getting the height value of this div
. In this case, there is no CSS pre-determining its height, so the height varies according to its content.
Code jQuery I’m using:
$(window).load(function(){
var alturaDivTxt2 = $(".slide-txt").height();
var autoH2 = $(".autoH-banner").css("height");
var alturaDoBanner2 = (autoH2.replace("px","") / 2) + 60;
var alturaTxtFinal2 = alturaDoBanner2 - alturaDivTxt2;
alert('div: '+alturaDivTxt2+' banner: '+alturaDoBanner2+' final: '+alturaTxtFinal2);
$(".slide-txt").css({ "margin-top": +alturaTxtFinal2+"px" });
});
I put that alert
just to see if the data was being processed correctly.
The value of alturaDivTxt2
returns zero.
What could be wrong? I’ve tried for that Function within the $(document).ready()
but it still doesn’t work. I’ve also tried using .innerHeight()
and other derivatives without success.
Instead of
$(window).load
change to$(document).ready
or$(function(){/*seucodigo*/});
– Diego Vieira
I’ve tried using inside the
$(document).ready
, it still didn’t work.– Ricardo
tries to recover thus http://jsfiddle.net/dieegov/4sc0pm4w/
– Diego Vieira
@Neither did Diegovieira. :(
– Ricardo
Do the following checks on the HTML code, the div has the class ". slide-txt"? Also check by inspecting element if the div has height. Put the HTML code structure if possible, also the css that matches the Divs this can help.
– Diego Vieira
That’s all right. Here’s an example: http://jsfiddle.net/jhcapr0n/. That’s weird, because I re-use that same code with
$(window).on('resize', function(){
and it works.– Ricardo
Is there any other element with the class
slide-txt
?– bfavaretto
No, it’s a
div
only used on the site banner.– Ricardo