Home › Forums › JavaScript › Function not being updated correctly
- This topic is empty.
-
AuthorPosts
-
December 26, 2015 at 5:12 pm #236168
xvilo
ParticipantHello,
I have been busy implementing the Dynamic page reload tutorial into my own web project. But I have a little CSS done in Javascript. Unfortunately the function is not being called correctly.
this is my code now:
$(function() { if(Modernizr.history){ var newHash = "", $mainContent = $("#main-content"), $pageWrap = $("#page-wrap"), baseHeight = 0, $el; $pageWrap.height($pageWrap.height()); baseHeight = $pageWrap.height() - $mainContent.height(); $("ul.mainnav").delegate("a", "click", function() { _link = $(this).attr("href"); history.pushState(null, null, _link); loadContent(_link); return false; }); function loadContent(href){ $mainContent .find("#guts") .fadeOut(200, function() { $mainContent.hide().load(href + " #guts", function() { $mainContent.fadeIn(200, function() { $pageWrap.animate({ height: baseHeight + $mainContent.height() + "px" }); }); $("ul.mainnav a").removeClass("current"); $("ul.mainnav a[href$='"+href+"']").addClass("current"); }); }); } $(window).bind('popstate', function(){ _link = location.pathname.replace(/^.*[\\\/]/, ''); loadContent(_link); }); console.log('begin function'); calcLeftWrapper(); console.log('end function'); } }); $( document ).ready(calcLeftWrapper()); $(window).resize(function(){ calcLeftWrapper(); }); function calcLeftWrapper(){ var divWidth = $('.original-width').width(); var divPosit = $('.original-width').offset(); var vph = $(window).height(); var element = '.leftwrapper'; var height = vph - divPosit.top - 15; console.log(divWidth+'px en '+divPosit.left+'px'); $(element).css("width", divWidth ); $(element).css("left", divPosit.left ); $(element).css("top", divPosit.top ); $(element).css("height", height); $(element).css("display", "block" ); }As you can see I added
calcLeftWrapper();
to the bottom and its being called on awindow.resize
and adocument.ready
but at the end of the original js part i have again addedcalcLeftWrapper();
within to console.log’s for debugging. I can see both console.log’s and alsoconsole.log(divWidth+'px en '+divPosit.left+'px');
is visible. But the CSS properties are not updated. As soon as I resize it’s there…What is going or am I doing wrong?
//xvilo
December 26, 2015 at 6:45 pm #236171Alen
Participant
/* $( document ).ready(calcLeftWrapper()); */ /* should be */ $( document ).on('ready', function(){ calcLeftWrapper(); }); /* or */ $( document ).ready(function(){ calcLeftWrapper(); });December 27, 2015 at 3:26 am #236172Shikkediel
ParticipantI don’t think
ready
can be used in combination with.on
though. But this is an option as well :$(document).ready(calcLeftWrapper);
Or even :
$(function() {calcLeftWrapper()});
And for the subsequent event listener this is a bit shorter :
$(window).resize(calcLeftWrapper);
December 27, 2015 at 3:37 am #236174xvilo
ParticipantWell the resize and document.ready are working fine. The 3rd call between console.log(‘Begin function’); and end function. At that one the css is not being updated….
December 27, 2015 at 4:01 am #236175Shikkediel
ParticipantCould you post a demo maybe then? Syntax looks correct, apart from some possible optimisations.
December 27, 2015 at 4:02 am #236176xvilo
Participanthttp://cp.thisisd3.com. It handles the left banner with lorem ipsum. But once you navigate it is not working
December 27, 2015 at 4:13 am #236177Shikkediel
ParticipantLink seems to be a dud…
December 27, 2015 at 4:20 am #236178xvilo
ParticipantAh, changed DNS settings yesterday! Should be all working now, at least it works here. Dont know how long DNS changes take at your end to propagate
December 27, 2015 at 4:25 am #236179Shikkediel
ParticipantNot yet, I guess. I’ll have another look later.
Edit – that doesn’t really make sense though, the main page is apparently there… any DNS changes should be globally simultaneous.
December 27, 2015 at 5:21 am #236185Alen
Participant$(document).ready();
is a shorthand for$(document).on('ready', fn);
@xvilo in the demo you linked to, it looks like it’s doing what its suppose to do. No? In addition, you can pass an object to CSS instead of reaching for the DOM every single time.$(element).css({ 'width': divWidth, 'left': divPosit.left, 'top': divPosit.top, 'height': height, 'display': 'block', });
December 27, 2015 at 5:28 am #236187xvilo
ParticipantAh i did not know that, but it is not working correctly. The element
.leftwrapper
has a standarddisplay:none
just to hide weird placement. After the page has been loaded and when its is resized it is being prepared with the right CSS. This explains:`$( document ).ready(calcLeftWrapper());
$(window).resize(function(){
calcLeftWrapper();
});`
But when you navigate, click on “Domein & Hosting” or “Contact” the same div should be recalculated and made visible, which does not happen.
December 27, 2015 at 5:34 am #236189Shikkediel
Participant$(document).ready();
is a shorthand for$(document).on('ready', fn);
I think it’s a bit more subtle than that, although I guess it would generally work (even if deprecated).
http://stackoverflow.com/q/13589307/3168107
One doesn’t really need to add quotes around the properties in the chained CSS by the way…
P.S. I can see the page now, strange.
December 27, 2015 at 6:43 am #236191Shikkediel
ParticipantLooks to me that it’s related to synchronicity. You could try putting the
calcLeftWrapper
function inside the callback of thefadeIn
.There are superfluous
body
andhtml
tags in the markup by the way.December 27, 2015 at 6:49 am #236192Alen
Participant@shikkediel didn’t know that was being removed. Just shows how much I’ve been working with jQuery, mostly using VueJS.
@xvilo I’ve replicated your issue by creating locally:index.html
and copy your markupdemo.html
so I can navigate between pages (same markup)- added following JavaScript (re-wrote some parts)
$(document).ready(function() { calcLeftWrapper(); }); $(window).resize(function() { calcLeftWrapper(); }); function calcLeftWrapper() { var $original = $('.original-width'); var $divWidth = $original.width(); var $divPosit = $original.offset(); var $vph = $(window).height(); var $element = $('.leftwrapper'); var $height = $vph - $divPosit.top - 15; $element.css({ 'width': $divWidth, 'left': $divPosit.left, 'top': $divPosit.top, 'height': $height, 'display': 'block', }); }
Everything is working as expected.
Hope that helps,
AlenDecember 27, 2015 at 6:54 am #236194Shikkediel
ParticipantTricky bit is that he seems to be using an Ajax call to replace the content again after doc ready – including the relevant element.
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.