Enjoy an ad free experience by logging in. Not a member yet? Register.
|
|
Results 1 to 15 of 20
Thread: Inaccurate width onload issue
-
03-11-2016, 09:50 AM #1Regular Coder
- Join Date
- Feb 2016
- Location
- NYC
- Posts
- 117
- Thanks
- 2
- Thanked 12 Times in 12 Posts
Inaccurate width onload issue
I'm sure this is a common bug, the CSS is marked at 100% width for the container but it does not match the given width of it's parent. It fixes itself after resizing the browser.
Is there a way to fix this without using setInterval?:
Ex.
window.onload
313 onload innerWidth
301 container
window.onresize
306 onload innerWidth
306 container
And no, I do not want to use:
Math.max(document.container.clientWidth, window.innerWidth || 0);
Because I want it to be able to match that of any parent container.
Thx.Last edited by everlive; 03-11-2016 at 10:03 AM.
-
03-11-2016, 11:10 PM #2Master Coder
- Join Date
- Sep 2005
- Location
- Sydney, Australia
- Posts
- 8,055
- Thanks
- 2
- Thanked 808 Times in 797 Posts
Why are you tampering with the width from JavaScript in the first place. You should be able to get this working properly with CSS as that's what CSS is for.
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
Don't forget to start your JavaScript code with"use strict";which makes it easier to find errors in your code.
-
03-11-2016, 11:28 PM #3Regular Coder
- Join Date
- Feb 2016
- Location
- NYC
- Posts
- 117
- Thanks
- 2
- Thanked 12 Times in 12 Posts
The CSS is set in the .css file to 100% as I said. The issue I am having is that when the JavaScript loads it detects two different widths rather than same and remains so until I resize the browser.
CSS file:
Parent width 100%
^container width 100%
JavaScript console.log window.onload:
Parent width 313 pixels (100%)
^container width 301 pixels (96.17%)Last edited by everlive; 03-11-2016 at 11:35 PM.
-
03-12-2016, 12:40 AM #4Regular Coder
- Join Date
- Feb 2016
- Location
- Keene, NH
- Posts
- 311
- Thanks
- 0
- Thanked 42 Times in 40 Posts
Without seeing the site in question it's impossible to say, but is this behavior consistent across all browsers, or just a particular one?
I find it increasingly queer how some folks ask for help on something but then don't show us what's actually failing...From time to time the accessibility of websites must be refreshed with the blood of designers and owners; it is its natural manure.
http://www.cutcodedown.com
-
03-12-2016, 12:50 AM #5Master Coder
- Join Date
- Sep 2005
- Location
- Sydney, Australia
- Posts
- 8,055
- Thanks
- 2
- Thanked 808 Times in 797 Posts
Are you remembering to take into account that the width in CSS normally includes padding and borders to that the actual content width will almost always be smaller?
My best guess would be that setting padding to zero will fix the problem without any need for JavaScript but without seeing the code I am relying on the shards of my broken crystal ball for trying to figure it out.Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
Don't forget to start your JavaScript code with"use strict";which makes it easier to find errors in your code.
-
03-12-2016, 06:57 AM #6Regular Coder
- Join Date
- Feb 2016
- Location
- NYC
- Posts
- 117
- Thanks
- 2
- Thanked 12 Times in 12 Posts
: /
...
Seriously?
K, right then.
Vanilla JS
CSSCode:(function(){ "use strict"; //Window Triggers window.onload = function (){ defaultBreak(); placementBlock(); }; window.onresize = function(){ placementBlock(); }; //Placement function placementBlock(){ var mainWrap = document.getElementById('agility-set').clientWidth; console.log(window.innerWidth + " onload innerWidth"); console.log(mainWrap + " parent container"); }; };
K, guess I'm putting this on the list of things I'll have to figure out for myself.Code:body { margin:0; padding:0; } #agility-set { position:relative; width: 100%; pointer-events:none; margin:0; padding:0; outline: 0; box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; text-decoration:none; list-style:none; backface-visibility: hidden; -moz-backface-visibility: hidden; -webkit-backface-visibility: hidden; font-family:'arial', sans-serif; text-transform:uppercase; }Last edited by everlive; 03-12-2016 at 07:15 AM.
-
03-12-2016, 07:09 AM #7
The declaration block for body in your css is missing an opening brace, meaning that the rest of the css is ignored. I suspect that may fix your issue, unless I have misunderstood the problem.
-
03-12-2016, 07:15 AM #8Regular Coder
- Join Date
- Feb 2016
- Location
- NYC
- Posts
- 117
- Thanks
- 2
- Thanked 12 Times in 12 Posts
No, ignore that, I was editing the post and deleted by accident.
Corrected so no one else makes that assumption.
-
03-12-2016, 07:32 AM #9
In that case I can't see the problem. Using that code in both Chrome and Firefox and (adding the invoker for the IIFE) I get
on page load,1366 onload innerWidth
1366 parent container
1266 onload innerWidth
1266 parent container
on semi-minimize
and
on maximizing again1366 onload innerWidth
1366 parent container
maybe defaultBreak is playing up?
-
03-12-2016, 07:56 AM #10Regular Coder
- Join Date
- Feb 2016
- Location
- NYC
- Posts
- 117
- Thanks
- 2
- Thanked 12 Times in 12 Posts
No, the defaultBreak kills the default CSS layout by removing a single class and replacing it with a class that is connected to different CSS coding if JS is enabled in a browser.
I then have CSS for the raw layout as seen above.
-
03-12-2016, 08:02 AM #11Regular Coder
- Join Date
- Feb 2016
- Location
- NYC
- Posts
- 117
- Thanks
- 2
- Thanked 12 Times in 12 Posts
It's the height...
Forgot to mention that the child elements are all absolute div tags so the parent container has to auto assume height. But I just gave the parent a fixed height and overflow hidden and that removed the gap issue.
Does that make sense?
If this works, then I have to use JS to adjust the parent height with the absolute blocks correct? Or is their a CSS only way of fixing this like using ::after or something?Last edited by everlive; 03-12-2016 at 08:05 AM.
-
03-12-2016, 11:30 AM #12Regular Coder
- Join Date
- Feb 2016
- Location
- Keene, NH
- Posts
- 311
- Thanks
- 0
- Thanked 42 Times in 40 Posts
IHMO you're still not showing enough to answer you... since scripting and CSS is gibberish without the markup it's being applied to, otherwise any of the dozens of things that could be interfering cannot be ruled out... like whitespace dom-nodes interfering in certain builds of IE (hence my asking if the problem is browser specific), if any other CSS present is interfering, if invalid markup is interfering, etc, etc...
Typically if height trickery is fixing it, there's something wrong with the markup like an unclosed tag or invalid element placement.From time to time the accessibility of websites must be refreshed with the blood of designers and owners; it is its natural manure.
http://www.cutcodedown.com
-
03-12-2016, 09:17 PM #13Master Coder
- Join Date
- Sep 2005
- Location
- Sydney, Australia
- Posts
- 8,055
- Thanks
- 2
- Thanked 808 Times in 797 Posts
So based on the information so far provided the problem is most likely the absolute div tags. Most likely there is a better way to achieve the same effect without having them absolute but as you haven't said why you have them set a way that is almost never needed we don't know whether you really need them that way or whether there is a better way to achieve the same result that doesn't cause the problem.
Without complete information the only answers you are likely to get are going to be patches rather than proper solutions.Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
Don't forget to start your JavaScript code with"use strict";which makes it easier to find errors in your code.
-
03-12-2016, 09:46 PM #14Regular Coder
- Join Date
- Feb 2016
- Location
- NYC
- Posts
- 117
- Thanks
- 2
- Thanked 12 Times in 12 Posts
I am making a shaped gallery as a minor change to using quadrilaterals and require the child blocks to be positioned absolute for free form category filtering and moving the blocks around without negative margins. A lot of what I require done needs JavaScript as the bottom line and I am pushing some limits on what is available to HTML and browsers.
I believe "solved" the issue with the width by applying height:1px; and overflow:hidden; to the parent div tags. Which I admit I really do not like and would prefer a different solution, but for now it fixes the issue. I am actually having the opposite issue now when I refresh the page, the scrollbar obscures the right side a little.
I am happy with the result as it is right now but will come back to this at a later date along with some other clean up objectives. Right now, this plugin is 1.0 and I will build on it overtime if the market wants it.Last edited by everlive; 03-12-2016 at 09:50 PM.
-
03-13-2016, 03:18 AM #15Regular Coder
- Join Date
- Feb 2016
- Location
- Keene, NH
- Posts
- 311
- Thanks
- 0
- Thanked 42 Times in 40 Posts
"Shaped Gallery", "quadrilaterals", "free form category filtering"... *SIGH*
Pushing the limits? You've gone past them and are telling users to go plow themselves with that nonsense... That's the type of artsy "design" that really has no place on a website... so unless you're building a web application that will NOT be served directly in a browser from a site, well... I'd tell you to pitch the entire mess, PARTICULARLY if you can't make it work without scripting FIRST.
Though if height:1px (the holly hack) "fixed" anything, you likely have no doctype, invalid nesting, invalid markup, or aren't using relative/absolute's relationship properly... and/or are testing in a ridiculously outdated browser like IE7/earlier... actually, make that IE6 since IE7 has overflow as a haslayout trigger.
NOT that ANYONE could tell that from your complete LACK of showing anything meaningful.From time to time the accessibility of websites must be refreshed with the blood of designers and owners; it is its natural manure.
http://www.cutcodedown.com



Reply With Quote

