cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div class="one" id="one">Я отвечаю за медиазапросы</div>
<div class="two" id="two">А я за JavaScript</div>

<span>Моя ширина ровно 600px</span>

<br />
<br />
<br />
<br />
<br />
<br />

<br />
<br />
<br />

<br />

<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br /><br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
            
          
!
            
              body {
  margin: 0;
}

div {
  height: 60px;
  margin: 20px 0;
  font-size: 30px;
  background: red;
}

span {
  display: block;
  height: 30px;
  width: 600px;
  background: black;
}

@media screen and (min-width: 1px) and (max-width:600px) {
  .one {
    background: blue;
  }
}
            
          
!
            
              var one = $("#one");
var two = $("#two");

var span = $("span");

// получаем ширину скролбара
function getScrollbarWidth() {
    var div = $('<div style="width:50px; height:50px; overflow:hidden; position:absolute; top:-200px; left:-200px;"><div style="height:100px;"></div></div>').appendTo('body');
    var w1 = $('div', div).innerWidth();
    div.css('overflow-y', 'scroll');
    var w2 = $('div', div).innerWidth();
    $(div).remove();
    return (w1 - w2);
}

// проверяем влияет-ли скролбар на медиа-запросы
function getDifference() {
  var windowWidth = $(window).width();
  var scrollbarWidth = getScrollbarWidth();

  var query = 'screen and (min-width: 1px) and (max-width:' + (windowWidth + scrollbarWidth) + 'px)';
  
  if (window.matchMedia(query).matches) {
    return scrollbarWidth;
  }
  
  return 0;
}

var widthDiff = getDifference();
console.log(widthDiff);

$(window).on("resize", function() {
  var windowsWidht1 = $(window).width();
  var windowsWidht2 = window.innerWidth;


  //  изменяем контрольную точку, если знаем, что скролбар влияет на медиа-запросы
  if (windowsWidht1 <= 600 - widthDiff) {
    two.css({
      background: "blue"
    });
  } else {
    two.css({
      background: "red"
    });
  }
 
});

    span.on('click', function () {

        var windowsWidht1 = $(window).width();
        var windowsWidht2 = window.innerWidth;

        console.log("span (линейка 600px)\n" + "$(window).width(): " + windowsWidht1 + "\nwindow.innerWidth: " + windowsWidht2+"\n")
    })

            
          
!
999px
Loading ..................

Console