ฉันจะทำให้ jQuery ทำอะไรได้ถ้าความกว้างหน้าจอของฉันน้อยกว่า 960 พิกเซล รหัสด้านล่างจะเริ่มการแจ้งเตือนครั้งที่สองเสมอโดยไม่คำนึงถึงขนาดหน้าต่างของฉัน:
if (screen.width < 960) {
alert('Less than 960');
}
else {
alert('More than 960');
}
ฉันจะทำให้ jQuery ทำอะไรได้ถ้าความกว้างหน้าจอของฉันน้อยกว่า 960 พิกเซล รหัสด้านล่างจะเริ่มการแจ้งเตือนครั้งที่สองเสมอโดยไม่คำนึงถึงขนาดหน้าต่างของฉัน:
if (screen.width < 960) {
alert('Less than 960');
}
else {
alert('More than 960');
}
คำตอบ:
ใช้ jQuery เพื่อรับความกว้างของหน้าต่าง
if ($(window).width() < 960) {
alert('Less than 960');
}
else {
alert('More than 960');
}
คุณอาจต้องการรวมเข้ากับกิจกรรมการปรับขนาด:
$(window).resize(function() {
if ($(window).width() < 960) {
alert('Less than 960');
}
else {
alert('More than 960');
}
});
สำหรับ RJ:
var eventFired = 0;
if ($(window).width() < 960) {
alert('Less than 960');
}
else {
alert('More than 960');
eventFired = 1;
}
$(window).on('resize', function() {
if (!eventFired) {
if ($(window).width() < 960) {
alert('Less than 960 resize');
} else {
alert('More than 960 resize');
}
}
});
ฉันลองhttp://api.jquery.com/off/โดยไม่ประสบความสำเร็จฉันจึงไปที่การตั้งค่าสถานะ eventFired
combine it with
นั่นเป็นเหตุผลที่ผมบอกว่า
equalHeights
ฟังก์ชั่นการปรับขนาดอย่างไร แต่สำหรับความกว้างที่มากกว่า 768 เท่านั้นมันใช้งานได้ดี ขอบคุณ
ฉันแนะนำให้ไม่ใช้ jQuery สำหรับสิ่งดังกล่าวและดำเนินการต่อwindow.innerWidth
:
if (window.innerWidth < 960) {
doSomething();
}
คุณสามารถใช้แบบสอบถามสื่อด้วยจาวาสคริปต์
const mq = window.matchMedia( "(min-width: 960px)" );
if (mq.matches) {
alert("window width >= 960px");
} else {
alert("window width < 960px");
}
ฉันอยากจะแนะนำ (จำเป็นต้อง jQuery):
/*
* windowSize
* call this function to get windowSize any time
*/
function windowSize() {
windowHeight = window.innerHeight ? window.innerHeight : $(window).height();
windowWidth = window.innerWidth ? window.innerWidth : $(window).width();
}
//Init Function of init it wherever you like...
windowSize();
// For example, get window size on window resize
$(window).resize(function() {
windowSize();
console.log('width is :', windowWidth, 'Height is :', windowHeight);
if (windowWidth < 768) {
console.log('width is under 768px !');
}
});
เพิ่มใน CodePen: http://codepen.io/moabi/pen/QNRqpY?editors=0011
จากนั้นคุณสามารถรับความกว้างของหน้าต่างได้อย่างง่ายดายด้วย var: windowWidth และ Height ด้วย: windowHeight
มิฉะนั้นรับห้องสมุด js: http://wicky.nillia.ms/enquire.js/
// Adds and removes body class depending on screen width.
function screenClass() {
if($(window).innerWidth() > 960) {
$('body').addClass('big-screen').removeClass('small-screen');
} else {
$('body').addClass('small-screen').removeClass('big-screen');
}
}
// Fire.
screenClass();
// And recheck when window gets resized.
$(window).bind('resize',function(){
screenClass();
});
ใช้
$(window).width()
หรือ
$(document).width()
หรือ
$('body').width()
ฉันรู้ว่าฉันมาสายเพื่อตอบคำถามนี้ แต่ฉันหวังว่าจะเป็นประโยชน์สำหรับใครก็ตามที่มีปัญหาคล้ายกัน มันยังทำงานเมื่อรีเฟรชหน้าด้วยเหตุผลใดก็ตาม
$(document).ready(function(){
if ($(window).width() < 960 && $(window).load()) {
$("#up").hide();
}
if($(window).load()){
if ($(window).width() < 960) {
$("#up").hide();
}
}
$(window).resize(function() {
if ($(window).width() < 960 && $(window).load()) {
$("#up").hide();
}
else{
$("#up").show();
}
if($(window).load()){
if ($(window).width() < 960) {
$("#up").hide();
}
}
else{
$("#up").show();
}
});});
ลองรหัสนี้
if ($(window).width() < 960) {
alert('width is less than 960px');
}
else {
alert('More than 960');
}
if ($(window).width() < 960) {
alert('width is less than 960px');
}
else {
alert('More than 960');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
ไม่สิ่งนี้จะไม่ทำงาน สิ่งที่คุณต้องการคือนี่ !!!
ลองสิ่งนี้:
if (screen.width <= 960) {
alert('Less than 960');
} else if (screen.width >960) {
alert('More than 960');
}
document.body.clientWidth
ถ้าใช้ vanilla JS) คือ <= 960 (หมายถึงคำสั่ง if ที่นี่) ดังนั้นตัวเลือกอื่น ๆ เท่านั้น (ELSE คือ screen.width> 960) ดังนั้นจึงใช้อย่างอื่นหากนี่ซ้ำซ้อน ใช้document.body.clientWidth <=960 ? handleSmallerThan960() : handleLargerThan960()
หรือตัวแปรบางอย่าง ไม่จำเป็นต้องมีอย่างอื่นหาก (รหัสซ้ำซ้อน)