简体   繁体   English

不同类型的位置固定但结果相同

[英]Different type of position fixed but same result

I have code that it normally behaves but I want to implement momentum scroll.我有它通常行为的代码,但我想实现动量滚动。 For the momentum scroll to work, I need to get the position fixed at the body tag.为了使动量滚动起作用,我需要将位置固定在 body 标签上。 That is not a problem.那不是问题。 The problem occurs in different elements with absolute positioning and flex grids.问题发生在具有绝对定位和弹性网格的不同元素中。

Do you know a way that I can bypass the fixed positioning?你知道我可以绕过固定定位的方法吗?

I am using the butter.js library for implementing momentum scroll but I tested with this codepen and it gives the same result我正在使用 butter.js 库来实现动量滚动,但我用这个 codepen 进行了测试,它给出了相同的结果

const body = document.body;
const main = document.getElementById('main');

let sx = 0, // For scroll positions
    sy = 0;
let dx = sx, // For container positions
    dy = sy;


body.style.height = main.clientHeight + 'px';


main.style.position = 'fixed';
main.style.top = 0;
main.style.left = 0;

// Bind a scroll function
window.addEventListener('scroll', easeScroll);


function easeScroll() {
  
  sx = window.pageXOffset;
  sy = window.pageYOffset;
}


window.requestAnimationFrame(render);

function render(){
  //We calculate our container position by linear interpolation method
  dx = li(dx,sx,0.07);
  dy = li(dy,sy,0.07);
  
  dx = Math.floor(dx * 100) / 100;
  dy = Math.floor(dy * 100) / 100;
  
  
  main.style.transform = `translate3d(-${dx}px, -${dy}px, 0px)`;
 
  
  
  window.requestAnimationFrame(render);
}

function li(a, b, n) {
  return (1 - n) * a + n * b;
}

Like I said it is not a problem with a code or the the way it is typed.就像我说的,这不是代码或键入方式的问题。 I do not get error while handling it.处理它时我不会出错。 But this但是这个

main.style.position = 'fixed';

is the problem.是问题所在。 I do trying to connect to a body tag but it still same problem我确实尝试连接到 body 标签,但它仍然是同样的问题

Link to a codepen momentum scroll链接到 codepen动量滚动

this is an image that to show how does it mess up这是一张图片,显示它是如何搞砸的

Try changing the width of the 'main' element.尝试更改“主要”元素的宽度。 They are usually coded to fit only elements not the screen它们通常被编码为仅适合元素而不适合屏幕

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

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