简体   繁体   English

如何使用jQuery获得相对于屏幕大小的50%?

[英]How to get 50% relative to the screen size, using jquery?

This is what i have 这就是我所拥有的

$(window).scroll(function(){
  var y = $(window).scrollTop();

  if (y > 400 & y < 10000) {
    $("#font").css("top", 0 + $(window).scrollTop());
  }
});

The 400 should be 50% relative to screen size. 400应该是屏幕尺寸的50%。 And the 10000 100% 和10000 100%

You can use the innerHeight (or use outerHeight to get the height of the complete browser window) property of the window object to determin the screen size. 您可以使用窗口对象的innerHeight (或使用outerHeight获取完整的浏览器窗口的高度)属性来确定屏幕尺寸。 This will return the height of the visible part of the page: 这将返回页面可见部分的高度:

$(window).scroll(function(){
  var y = $(window).scrollTop();

  var intViewportHeight = window.innerHeight;

  if (y > (intViewportHeight * 0.5) & y < (intViewportHeight * 1)) {
    $("#font").css("top", 0 + $(window).scrollTop());
  }
});

PS: you obviously can omit the * 1 part of the second formula. PS:您显然可以省略第二个公式的* 1部分。 I just used it for clarification of the 100% 我只是用它来澄清100%

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

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