jQuery check if browser support position: fixed
The most reliable way would be to actually feature-test it. Browser sniffing is fragile and unreliable.
I have an example of such test in CFT http://kangax.github.com/cft/#IS_POSITION_FIXED_SUPPORTED. Note that the test should be run after document.body
is loaded.
Checking the support for a css property attribute
function isFixedSupported() {
var isSupported = null;
if (document.createElement) {
var el = document.createElement("div");
if (el && el.style) {
el.style.position = "fixed";
el.style.top = "10px";
var root = document.body;
if (root && root.appendChild && root.removeChild) {
root.appendChild(el);
isSupported = el.offsetTop === 10;
root.removeChild(el);
}
}
}
return isSupported;
}
var canUseFixed = isFixedSupported(); //true:false
FIDDLE
How to keep content from overlapping with fixed nav-bar without using padding
Try adding an high z-index value to your navbar. This way It should always be on top of other elements.
Find mouse position relative to element
For people using JQuery:
Sometimes, when you have nested elements, one of them with the event attached to it, it can be confusing to understand what your browser sees as the parent. Here, you can specify which parent.
You take the mouse position, and then subtract it from the parent element's offset position.
var x = evt.pageX - $('#element').offset().left;
var y = evt.pageY - $('#element').offset().top;
If you're trying to get the mouse position on a page inside a scrolling pane:
var x = (evt.pageX - $('#element').offset().left) + self.frame.scrollLeft();
var y = (evt.pageY - $('#element').offset().top) + self.frame.scrollTop();
Or the position relative to the page:
var x = (evt.pageX - $('#element').offset().left) + $(window).scrollLeft();
var y = (evt.pageY - $('#element').offset().top) + $(window).scrollTop();
Note the following performance optimisation:
var offset = $('#element').offset();
// Then refer to
var x = evt.pageX - offset.left;
In this way, JQuery does not have to look up #element
for each line.
Update
There is a newer, JavaScript-only version in an answer by @anytimecoder -- see also browser support for getBoundingClientRect().
Check if element is visible in DOM
According to this MDN documentation, an element's offsetParent
property will return null
whenever it, or any of its parents, is hidden via the display style property. Just make sure that the element isn't fixed. A script to check this, if you have no position: fixed;
elements on your page, might look like:
// Where el is the DOM element you'd like to test for visibility
function isHidden(el) {
return (el.offsetParent === null)
}
On the other hand, if you do have position fixed elements that might get caught in this search, you will sadly (and slowly) have to use window.getComputedStyle()
. The function in that case might be:
// Where el is the DOM element you'd like to test for visibility
function isHidden(el) {
var style = window.getComputedStyle(el);
return (style.display === 'none')
}
Option #2 is probably a little more straightforward since it accounts for more edge cases, but I bet its a good deal slower, too, so if you have to repeat this operation many times, best to probably avoid it.
Related Topics
Display Twitter Bootstrap Btn-Group Inline with Text
Safari Image Size Auto Height CSS
Bootstrap 4 CSS Causing Chrome's Print "Layout" to Disappear
Align Block Elements on Top When Using Line-Height
Remove Border Around Sprite Image in Chrome
CSS Selector for First Element of Visual (Block Reflow) Row
Is Anyone Experiencing Layout Issues After Upgrading to Chrome 72
:Empty Pseudo Class Issue with Added/Removed Content and Sibling Combinators
Add Padding Without Changing Overall Width
Selector for Even and Odd Level Children
Viewport for Ie10 & 11 Desktop, But Not Mobile
Appbar Overlaps with Other Elements
CSS Url() Not Recognized in Internet Explorer 10
CSS How to Change Opacity of Container But Not Text
Reliable Solution for Conditional Comments in Outlook.Com HTML Emails