admin管理员组文章数量:1315782
I know little to nothing about JavaScript so I'm not even sure if my question makes sense. My point however is how to apply the same principle of media queries to change particular values on a script depending on the width of the screen.
In the following example, I'm using a scroll spy active menu with a negative scrolling of -100px for Header pensation. This offset is necessary because the header has a fixed position.
I need the offset value to change to 0px if window is smaller then 900px width, because at that point, Header position bees relative.
$(document).ready(function () {
$(window).scroll(function () {
var y = $(this).scrollTop();
$('.link').each(function (event) {
if (y >= $($(this).attr('href')).offset().top - 100) {
$('.link').not(this).removeClass('active');
$(this).addClass('active');
}
});
});
});
$(function () {
$('a[href*=#]:not([href=#])').click(function () {
if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
if (target.length) {
$('html,body').animate({
scrollTop: (target.offset().top - 100)
}, 850);
return false;
}
}
});
});
I know little to nothing about JavaScript so I'm not even sure if my question makes sense. My point however is how to apply the same principle of media queries to change particular values on a script depending on the width of the screen.
In the following example, I'm using a scroll spy active menu with a negative scrolling of -100px for Header pensation. This offset is necessary because the header has a fixed position.
I need the offset value to change to 0px if window is smaller then 900px width, because at that point, Header position bees relative.
$(document).ready(function () {
$(window).scroll(function () {
var y = $(this).scrollTop();
$('.link').each(function (event) {
if (y >= $($(this).attr('href')).offset().top - 100) {
$('.link').not(this).removeClass('active');
$(this).addClass('active');
}
});
});
});
$(function () {
$('a[href*=#]:not([href=#])').click(function () {
if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
if (target.length) {
$('html,body').animate({
scrollTop: (target.offset().top - 100)
}, 850);
return false;
}
}
});
});
Share Improve this question asked Jan 23, 2015 at 14:14 David MartinsDavid Martins 2,0566 gold badges26 silver badges34 bronze badges 3-
$(window).on('resize',function() { if ($(window).width < 900) ... })
– Blazemonger Commented Jan 23, 2015 at 14:16 - 5 Why not use a CSS media query for this? I assume that the header is set to fixed via CSS at that resolution anyway? – Rory McCrossan Commented Jan 23, 2015 at 14:16
- @Blazemonger Thank you for your quick reply. I've tried your suggestion but I'm afraid I'm not doing it right. Would you be so kind as showing an example in context with the code I provided? Thanks a lot. – David Martins Commented Jan 23, 2015 at 14:39
4 Answers
Reset to default 5You actually do have something similar to Media Queries in JS:
if (matchMedia("(min-width: 900px)").matches) {
// the viewport is at least 900 pixels wide
}
You can define .resolution-check
class in css when to be visible:
@media only screen and (max-width: 900px) {
.resolution-check {
display: block;
}
}
and then simply check in JS if that element is visible:
if ($('.resolution-check').is(':visible')) {
// do stuff here
}
This way you can control media queries in one place, in CSS
function checkMatchMedia(pQuery) {
if (!window.matchMedia) {return false;}
if (window.matchMedia(pQuery).matches) {return true;}
return false;
}
var vIsOK = checkMatchMedia("(min-width: 500px)");
if (vIsOK) {
console.log('window width is at least 500px');
} else {
if (window.matchMedia) {
console.log('window width is less then 500px');
} else {
console.log('Please include Polyfill.');
}
}
There is only a small problem with this, that is that older IE do not supports this.
This fixed with adding a libraries named polyfill .
How to use Media Queries in JavaScript and replace div or image or text Proto Theme Logo Change on mobile
jQuery(function($){
if (window.matchMedia("(max-width: 500px)").matches) {
$( ".vc_responsive .logo > a > img " ).replaceWith( "<img class='img-responsive standard-logo' src='your URL' alt='Xtreme'>" );
}
});
本文标签: jqueryHow to use Media Queries in JavaScriptStack Overflow
版权声明:本文标题:jquery - How to use Media Queries in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741968902a2407713.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论