简体   繁体   English

在媒体查询JavaScript中使用滚动顶部

[英]Using scrolltop in media query javascript

I am tuning my scrolltop for mobiles and tablet. 我正在调整手机和平板电脑的滚动条。 But i noticed, when i have this code for desktop: 但是我注意到,当我在桌面上有以下代码时:

$(document).ready(function(){
    "use strict";
    $('#menu ul li #page3').on('click', function(){
        $('html,body').animate({scrollTop: $(".footer").offset().top  * 0.99}, 850);
    }); 
}); 

When i use: 当我使用时:

var mql = window.matchMedia("only screen and (min-device-width: 320px) and (max-device-width: 812px) and (orientation : portrait) ");
if (mql.matches){ // ALL PHONES

$(document).ready(function(){
    "use strict";
    $('#menu ul li #page3').on('click', function(){
        $('html,body').animate({scrollTop: $(".footer").offset().top  * 0.99}, 850);
    }); 
}); 

}

When i use another value for offset, it doesn't go directly to #page3, it goes first to the old #page 3 settings from desktop, STOPS and then go slowly to the #page3 set in the media query 当我使用其他值作为偏移量时,它不会直接转到#page3,而是会先从桌面转到STOPS的旧#page 3设置,然后再慢慢转到媒体查询中设置的#page3

Why isn't this just replacing the code like with css media query's? 为什么这不只是用CSS媒体查询代替代码?

Another problem is that the media query in javascript does work but when i rotate my screen from landscape to portrait or the other way around it still uses the landscape code in portrait mode and the other way. 另一个问题是javascript中的媒体查询确实可以正常工作,但是当我将屏幕从横向旋转为纵向或以其他方式旋转时,它仍以纵向和其他方式使用横向代码。 Does someone know how to fix this? 有人知道如何解决这个问题吗?

The last thing is that nothing changes when i target my ipad: 最后一件事是,当我定位到ipad时,什么都没有改变:

var mql = window.matchMedia("only screen and (min-device-width: 768px) and (max-device-width: 1366px) and (orientation : landscape) ");
if (mql.matches){ //TABLET LANDSCAPE

}

Help and explanation is appreciated 帮助和解释表示赞赏

EDIT: 编辑:

I am using this: 我正在使用这个:

var mql = window.matchMedia("only screen and (min-device-width: 320px) and (max-device-width: 812px) and (orientation : portrait) ");
if (mql.matches){ // ALL PHONES PORTRAIT

But want to use also this in it 但也要在其中使用

var mql = window.matchMedia("only screen and (min-device-width: 320px) and (max-device-width: 812px) and (orientation : landscape) ");
    if (mql.matches){ // ALL PHONES LANDSCAPE

How can i combine these 2 in one code for IF, and let the desktop have ELSE. 我如何才能将这2个代码合并为IF代码,并让桌面具有ELSE。

That with the desktop worked and it also worked with 1 IF, but doesn't work with both. 在台式机上可以使用,并且在1 IF下也可以使用,但不能同时使用。

Can you also combine the window resize code inside my code for me to understand completely how it works because i know 0,0 about this. 您是否也可以将窗口调整大小代码合并到我的代码中,以使我完全了解它的工作原理,因为我对此知道0,0。 I just know how to use multiple media queries in javascript underneath eachother, that's it. 我只知道如何在彼此之间的javascript中使用多个媒体查询,仅此而已。

I really appreciate your time and effort. 非常感谢您的投入和时间。

  1. It's natural, you have two functions to call when something was happened (click) so both of them must be call one by one, it wouldn't override the first one - think, how many problems it could generate? 很自然,当发生某事(单击)时,您有两个函数可以调用,因此必须同时调用这两个函数,这不会覆盖第一个函数-想想,它可能产生多少问题? Let's say you have some slider with control buttons and then you want to change some section content when click on some of this buttons - so you add some action to click event on control buttons - if this code could override the old one, then you wouldn't be able to show next slide. 假设您有一些带有控制按钮的滑块,然后想要在单击某些按钮时更改部分内容-因此,您要在控制按钮上的click事件中添加一些操作-如果此代码可以覆盖旧代码,那么您会无法显示下一张幻灯片。 Not good at all :/ In your case you can prevent double call by just place code for desktop inside 'else'. 一点都不好:/在您的情况下,只需将桌面代码放在“ else”中,就可以防止重复调用。

     if (mql.matches) { //code for mobiles } else { //code for desktop } 
  2. Problem with landscape is because your javascript code keeps the 'mql' value calculated after page load, which is not updated after screen rotating. 横向问题是因为您的JavaScript代码保留了页面加载后计算的“ mql”值,屏幕旋转后该值不会更新。 To fix that you can add some event handler for window resize (rotate phone changes window size) and update 'mql' value then: 要解决此问题,您可以为窗口调整大小(旋转手机更改窗口大小)添加一些事件处理程序,然后更新“ mql”值:

     $(window).resize(function() { mql = window.matchMedia("only screen and (min-device-width: 320px) and (max-device-width: 812px) and (orientation : portrait) "); }); 

    Tip: to improve your code you can move this device match code to some separate function. 提示:为改善代码,您可以将此设备匹配代码移至某些单独的功能。

  3. I don't have much experience with iPads, but maybe they have bigger resolution than in your query? 我没有使用iPad的丰富经验,但也许它们的分辨率比您的查询还要大?

EDIT: 编辑:

  1. Your code should look similar to this: 您的代码应类似于以下内容:

     $(document).ready(function() { function mediaSize() { var isTabletLandscape = window.matchMedia('(min-width: 768px) and (max-width: 1366px) and (orientation: landscape)'); var isPhoneLandscape = window.matchMedia('(min-width: 320px) and (max-width: 812px) and (orientation: landscape)'); var isPhonePortrait = window.matchMedia('(min-width: 320px) and (max-width: 812px) and (orientation: portrait)'); if (isTabletLandscape.matches) { //code for tablet landscape } else if (isPhoneLandscape.matches) { //code for phone landscape } else if (isPhonePortrait.matches) { //code for phone portrait } else { //code for desktop $('#menu ul li #page3').on('click', function(){ $('html,body').animate({scrollTop: $(".footer").offset().top * 0.99}, 850); }); } } mediaSize(); window.addEventListener('resize', mediaSize, false); }); 

First off this is a horrid selector and really makes no sense to have an id, then another id like this. 首先,这是一个可怕的选择器,拥有一个ID毫无意义,然后再有一个这样的ID。 $('#menu ul li #page3') I assume you want to do something based on the click of some LI (or link inside that, but for simplicity we will use li). $('#menu ul li #page3')我假设您想基于某些LI的单击(或其中的链接,但为简单起见,我们将使用li)来做某事。 Note that you did not provide HTML however I suspect you have duplicate id's ie id="page3" which is invalid html, don't do that. 请注意,您没有提供HTML,但是我怀疑您有重复的ID,即id="page3" ,它是无效的html,请不要这样做。

Next we need to use the media query to set a boolean value group for each. 接下来,我们需要使用媒体查询为每个查询设置一个布尔值组。 Now that we know those, we can use a conditional (if) to do something. 既然我们知道了这些,我们就可以使用条件(if)来做某事。 Here we will set a size value and use it. 在这里,我们将设置一个大小值并使用它。

Now that we have ironed out that, we can write a function to return a value based on those. 现在我们已经解决了这一问题,我们可以编写一个函数以基于这些值返回一个值。 We will call that function within the single click event handler. 我们将在单击事件处理程序中调用该函数。 I am not sure of your desire here as it is not super clear in your question so I will just put it together and you can later alter this to your specific needs. 我不确定您在这里的愿望是什么,因为您的问题还不太清楚,所以我将其汇总在一起,以后您可以根据自己的特定需求进行更改。

 function sniffTheMedia() { let isIpad = window.matchMedia("only screen and (min-device-width: 768px) and (max-device-width: 1366px) and (orientation : landscape) "); let isLandscape = window.matchMedia("only screen and (min-device-width: 320px) and (max-device-width: 812px) and (orientation : landscape) "); let isPortrait = window.matchMedia("only screen and (min-device-width: 320px) and (max-device-width: 812px) and (orientation : portrait) "); // I picked totally random numbers, adjust to suit let size = isIpad ? 800 : isLandscape ? 400 : isPortrait ? 300 : 500; return size; } // add event handler $(function() { "use strict"; $('#menu').on('click', 'li', function() { let sniffed = sniffTheMedia(); //console.log(sniffed); let target = "#" + $(this).data("target"); $('html, body').animate({ scrollTop: $(target).offset().top * 0.99 }, sniffed); }); }); 
 .somediv { height: 6em; border: solid cyan 1px; } .footer { height: 5em; } .men-item { border: solid lime 1px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="menu"> <ul class="menu-group"> <li class="menu-item" data-target="page1">Howdy 1</li> <li class="menu-item" data-target="page2">Friend 2</li> <li class="menu-item" data-target="page3">Friend 3</li> <li class="menu-item" data-target="myfooter">footer</li> </ul> </div> <div id="page1" class="somediv">I am a div content 1</div> <div id="page2" class="somediv">I am a div content 2</div> <div id="page3" class="somediv">I am a div content Page 3</div> <div id="myfooter" class="footer"><span>Footer Content</span> I am in the footer </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM