i am looking to apply media queries through javascript -
i want slide divs of menu left right,i have done using javascript
<div id="script" style="height: 250px;"> <div class="slide"> <div id="div1" class="slide-item"> <p>home</p> </div> </div> <div class="slide"> <div id="div2" class="slide-item"> <p>about us</p> </div> </div> <div class="slide"> <div id="div3" class="slide-item"> <p>events</p> </div> </div> <div class="slide"> <div id="div4" class="slide-item"> <p>register</p> </div> </div> </div>
but cant different media screen have tried this:
document.ready(function () { if (screen.width <= 450) { $("#div1").animate({ left: '100px' }, { duration: 500 }); } else if (screen.width >= 451 && screen.width <= 700) { $("#div1").animate({ left: '150px' }, { duration: 500 }); } else (screen.width > 700) { $("#div1").animate({ left: '200px' }, { duration: 500 }); }
i have tried using switch case:
$(document).ready(function () { switch ( varmql) { case window.matchmedia("screen , (min-width: 450px)"): $("#div1").animate({ left: '60px' }, { duration: 500 }); break; case window.matchmedia("screen , (min-width: 451px) , (max-width:540px)"): $("#div1").animate({ left: '80px' }, { duration: 500 }); break; case window.matchmedia("screen , (min-width: 541px) , (max-width:768px)"): $("#div1").animate({ left: '100px' }, { duration: 500 }); break; case window.matchmedia("screen , (min-width: 769px) , (max-width:1024px)"): $("#div1").animate({ left: '140px' }, { duration: 500 }); break; case window.matchmedia("screen , (min-width: 1025px) , (max-width:1200px)"): $("#div1").animate({ left: '160px' }, { duration: 500 }); break; case window.matchmedia("screen , (min-width: 1201px) "): $("#div1").animate({ left: '180px' }, { duration: 500 }); default } });
doing slide item not appearing @ on screen
look @ documentation matchmedia
@ https://developer.mozilla.org/en-us/docs/web/api/window/matchmedia.
it returns "media query list" on have access matches
property:
case window.matchmedia("screen , (min-width: 450px)").matches: ^^^^^^^^
i don't know varmql
is, seem should replace true
.
but commenter mentioned, don't need @ all. can use css media queries:
#div1 { transition: 500ms; } @media screen , (min-width: 450px) { #div1 .animate { left: 60px; } } $(document).ready(function() { $('#div1').addclass('animate'); });
or similar.
Comments
Post a Comment