Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Find current breakpoint with Javascript #25124

Closed
mukhammad-akilov opened this issue Dec 29, 2017 · 13 comments
Closed

Feature Request: Find current breakpoint with Javascript #25124

mukhammad-akilov opened this issue Dec 29, 2017 · 13 comments

Comments

@mukhammad-akilov
Copy link

mukhammad-akilov commented Dec 29, 2017

It would be very useful if we could find out what breakpoint is being displayed at the moment with Javascript.

For example:

if( bootstrapBreakpoint() == "xs" ) { // do something } else { // do something }

@Herst
Copy link
Contributor

Herst commented Dec 30, 2017

I remember this being asked for Bootstrap 3 numerous times, see https://stackoverflow.com/questions/18575582/how-to-detect-responsive-breakpoints-of-twitter-bootstrap-3-using-javascript (the answer https://stackoverflow.com/a/37141090 is even about BS4).

The question is, is this even a good idea to begin with? What if the breakpoints change? Also, why not simply check the visibility/size of a particular element if what you need it for is tied to one?

@patrickhlauke
Copy link
Member

patrickhlauke commented Dec 30, 2017

fwiw, this can be achieved with something like

var lg = window.getComputedStyle(document.documentElement).getPropertyValue('--breakpoint-lg');
if (window.matchMedia("(min-width: "+lg+")").matches) {
  /* do something */
} else {
  /* do something else */
}

(not exhaustively tested, so ymmv)

[edit: and for reference, it's not possible to use var(--breakpoint-lg) directly in the query tested by matchMedia, as var() is not allowed in that context - hence the slightly awkward acrobatics]

@mukhammad-akilov
Copy link
Author

@Herst If the breakpoints change i can detect current breakpoint wiht jQuery .resize() handler.

For exampe:

$( window ).resize(function() { if( bootstrapBreakpoint() == "xs" ) { // do something } else { // do something }; });

An example of where we can use:

I want to set li > a line-height like .navbar-brand image height (whose height is unknown to me
so i can't set line-hiehgt's value in css file) only in lg screen because on other breakpoints navbar is collapsed so line-height value must be 1.5.

$( window ).resize(function() { if (bootstrapBreakpoint() == "lg") { $(".navbar-nav > li > a").css( "line-height", navbarBrandHeight + "px" ); } else { $(".navbar-nav > li > a").css("line-height", "1.5"); } });

@patrickhlauke
Copy link
Member

I want to set li > a line-height...

why do you want to do this in JS, when you could simply do it with a few lines of CSS wrapped in a media query?

@mdo
Copy link
Member

mdo commented Dec 31, 2017

I imagine there are also other CSS solutions you could explore. For example, if you want an image and text to be vertically centered with one another, you can use flexbox. Set the parent to .d-flex.align-items-center if it isn't already, and have two children elements: <img> and <span>.

@mdo
Copy link
Member

mdo commented Dec 31, 2017

Closing out though—no plans to add this ourselves.

@Johann-S
Copy link
Member

Johann-S commented Oct 5, 2018

I don't know if it'll help someone, but I made a plugin which help you to detect what's the current Bootstrap v4 breakpoint, see: https://github.com/Johann-S/bs-breakpoints

@alixbergeret
Copy link

I am finding it very surprising that this is not available, and that people seem to be saying "why would you need that". I am finding myself having to refresh a Google Chart graph when the break-point changes, so that the graph fits the new width of its container, and it's really surprising that there isn't a simple was of doing that. You should seriously consider adding it in.

@Johann-S
Copy link
Member

Johann-S commented Apr 1, 2019

@alixbergeret this plugin https://www.npmjs.com/package/bs-breakpoints do exactly what you want

@alixbergeret
Copy link

@alixbergeret this plugin https://www.npmjs.com/package/bs-breakpoints do exactly what you want

Hi,

Thank you.

I have tried it, but I can't get it to work. I have used the code extract from the plug-in's page, here:
https://mi-linux.wlv.ac.uk/~in9352/bootstrap/alix.html
Look at line 25 onwards. Nothing in the console. Events don't seem to be detected at all. What am I doing wrong?

Also, it would be nice to have this as standard in Bootstrap, and not to have to include yet another library... but thanks for sharing anyway :)

@Johann-S
Copy link
Member

Johann-S commented Apr 2, 2019

Hi @alixbergeret that's because bsBreakpoints detect jquery and dispatch the events through jQuery system.

See my CodePen based on your code: https://codepen.io/Johann-S/pen/mgJEKB

BTW if you have any issues with bsBreakpoints you can post an issue here: https://github.com/Johann-S/bs-breakpoints/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc

@alixbergeret
Copy link

Hi @alixbergeret that's because bsBreakpoints detect jquery and dispatch the events through jQuery system.

See my CodePen based on your code: https://codepen.io/Johann-S/pen/mgJEKB

BTW if you have any issues with bsBreakpoints you can post an issue here: https://github.com/Johann-S/bs-breakpoints/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc

Brilliant, that worked. Sorry I didn't realise you couldn't detect the events both ways. I can now redraw my Google Charts on breakpoint change, meaning they always fit their container :) thanks for taking the time.

@CttCJim
Copy link

CttCJim commented Jul 1, 2020

I've been looking at solutions for this and they are all very convoluted. If you are using JQuery, it's pretty simple.

In your HTML:
<div id="visXS" class="visible-xs"></div> \<div id="visSM" class="visible-sm"></div> \<div id="visMD" class="visible-md"></div> \<div id="visLG" class="visible-lg"></div> \<div id="visXS" class="visible-xl"></div>
(remove all "" from the above; this comment box doesnt like HTML.)

In your JS:
function getSize() { if($("#visXS").is(":visible")) {return "xs"} if($("#visSM").is(":visible")) {return "sm"} if($("#visMD").is(":visible")) {return "md"} if($("#visLG").is(":visible")) {return "lg"} if($("#visXL").is(":visible")) {return "xl"} }

window.onresize = (function() { console.log(getSize()); });

I find it remarkable how many people say "why would you want your JS to do this when your CSS can?" Guys, it's his business why. I wanted it so I could make my bootstrapTable3 toggle views automatically in SM/XS. Can't do that in CSS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants