为什么Android上需要使用viewport元标记来获取此代码?

时间:2017-03-17 20:19:02

标签: javascript android jquery html css

更新2 - 18-03-2017:显然我需要使用window.devicePixelRatio。我还不知道到底是怎么回事。但这就是我现在需要弄清楚的。

我试图将300px(每个彩色块的高度设置)的CSS大小除以window.devicePixelRatio,但似乎我的计算结果不合适。

更新1 - 18-03-2017:我想我已经在某种程度上指出了这个问题:

最大scrollTop值远低于例如Chrome for Android比仅使用Chrome(适用于桌面版)。问题必须出在paddingTop的{​​{1}}。我需要以这样的方式填充空#scrolldistract,以便此高度或填充在Chrome for Android中注册,因为它会增加最大div值。现在只要我使用scrollTopmin-height或具有一定像素高度的line-height等,它就会保持在540左右。

在我的代码中,我试图动态地获得一些高度和坐标。如果我使用HTML代码注释中指定的视口元标记,则所有内容都可以在Chrome for Android和Firefox for Android上使用,就像使用例如Chrome(桌面版)。

但是,当我没有插入视口元标记时,它会有错误的测量结果(在Chrome for Android和Firefox for Android上),即使我是动态请求它们。

我知道移动设备上有虚拟像素这样的东西:https://developer.mozilla.org/en-US/docs/Mozilla/Mobile/Viewport_meta_tag

但我不明白为什么浏览器没有返回正确的值(我需要使用的实际值)。我不太明白我在这里做错了什么?

为什么使用视口元标记?因为我想启用双击 - 由于300毫秒的延迟,这显然非常顽皮。我真的想要使用双击,因为需要缩放一些文本(以可视形式)。

嗯,当然还有缩放到缩放的手势,它仍然使用视口元标记启用(并且在使用或不包含视口元标记时也会出现一些问题)。但是有两个水龙头更适合这个目的。



table

var $window = $(window);
var $document = $(document);
// Element which needs to fade in and out.
var $fadingblack = $("#fadingblack");
var $scrolldistract = $("#scrolldistract");
var $scrollsviascrolldistract = $("#scrollsviascrolldistract");
// Pulls up the child divs of #scrollsviascrolldistract, under it.
var $puller = $("#puller");


// Start of fading area (Y-value).
var scrollTopStart = $fadingblack.position().top;
// And of course the Y-value of the end of the fading area.
var scrollTopEnd = scrollTopStart + $fadingblack.height();

// Maximum scrollTop-value (when scrollbar is at 100%).
var lastScrollTop = $document.height() - $window.height();

// Amount of scrolled pixels (vertically) including amount scrolled while
// the fading element is fading.
var scrollAmountWithFadeAmount = $document.height + $fadingblack.height();
// Setting height does not quite work for an empty div,
// so we are using some padding.
$scrolldistract.css("paddingTop", scrollAmountWithFadeAmount);
// Percentage of which we have scrolled (1 = 100%).
var currentScrollTopP;
// Current scrollTop value.
var realCurY;

$(function() {
  // Off you go code...

  function doScrollOrFade() {
    currentScrollTopP = Math.ceil($window.scrollTop() / lastScrollTop * 100) / 100;
    realCurY = currentScrollTopP * lastScrollTop;

    if (realCurY >= scrollTopStart && realCurY <= scrollTopEnd) {
      // Current realCurY dictates we are in fade area.
      // So scroll the fading area into view at top of browser viewport.
      $puller.css("marginTop", -scrollTopStart);
      // Determine opacity percentage.
      var fadePercent = (realCurY - scrollTopStart) / (scrollTopEnd - scrollTopStart);
      // Fade to current opacity immediately.
      $fadingblack.fadeTo(0, fadePercent);
    } else {
      // We are outside of the fading area and in scroll-mode.
      if (realCurY < scrollTopStart) {
        // We are somewhere before the fading area, so set opacity to 0.
        $fadingblack.fadeTo(0, 0);
      } else {
        // We are somewhere after the fading area, so set opacity to 1.
        $fadingblack.fadeTo(0, 1);
      }

      if (realCurY > scrollTopEnd) {
        // We have passed the fading area. So we have an amount
        // of pixels we wasted on the opacity changes.
        // Correct it here.
        $puller.css("marginTop", -realCurY + $fadingblack.height());
      } else {
        $puller.css("marginTop", -realCurY);
      }
    }
    window.requestAnimationFrame(doScrollOrFade);
  }

  window.requestAnimationFrame(doScrollOrFade);

  $window.on('resize orientationchange', function(e) {
    // On resize or orientation change recalculate some stuff.
    lastScrollTop = $document.height() - $window.height();
    scrollAmountWithFadeAmount = $document.height + $fadingblack.height();
    $scrolldistract.css("paddingTop", scrollAmountWithFadeAmount);
    window.requestAnimationFrame(doScrollOrFade);
  });
});
&#13;
body {
  background-color: whitesmoke;
}

#scrollsviascrolldistract {
  position: fixed;
  left: 0px;
  top: 0px;
  width: 100%;
}

#scrolldistract {
  position: absolute;
  left: 0px;
  top: 0px;
  width: 100%;
  padding-top: 2100px;
  height: 0px;
}

#puller {
  position: relative;
  margin-top: 0px;
  left: 0px;
  top: 0px;
}

img {
  display: block;
}

.black,
.red,
.blue {
  border: solid 1px yellow;
  font-size: 32pt;
  position: relative;
  width: 100%;
  height: 300px;
}

.red {
  background-color: red;
}

.blue {
  background-color: blue;
}

.black {
  background-color: black;
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

视口元标记用于将页面缩放到设备物理高度和显示宽度的大小;如果没有它,浏览器会加载页面并将其缩小以使其适合屏幕。因此理论上,没有视口的页面的高度和宽度与桌面

相同