我在不同的最新浏览器上测试了它,所以它不可能是兼容性问题。我正在使用带有样式组件的create-react-app,下面是我的代码:
import React, { Component } from 'react';
import styled, { createGlobalStyle } from 'styled-components';
const GlobalStyle = createGlobalStyle`
@import url('https://fonts.googleapis.com/css?family=Merriweather|Oleo+Script');
`
const Wrapper = styled.div`
scroll-snap-type: y mandatory;
display: flex;
flex-direction: column;
font-size: 16px;
`
const Home = styled.div`
scroll-snap-align: center;
height: 100vh;
background-color: yellow;
display: flex;
flex-direction: row;
`
const Menu = styled.div`
scroll-snap-align: center;
height: 100vh;
background-color: blue;
display: grid;
`
const Speciality = styled.div`
scroll-snap-align: center;
height: 100vh;
background-color: green;
`
class App extends Component {
render() {
return (
<Wrapper>
<GlobalStyle />
<Home />
<Menu />
<Speciality />
</Wrapper>
);
}
}
export default App;当我滚动的时候它什么也做不了,我几乎什么都试过了。如果没有样式化组件,它也不会工作。
编辑:我将代码复制到codesandbox:https://codesandbox.io/s/72jmn1p1w1
发布于 2020-04-28 05:14:12
不是将scroll-snap-type: y mandatory应用于父div,而是尝试将其应用于html:
html {scroll-snap-type: y mandatory}
发布于 2021-12-29 18:19:10
在你的代码沙箱中,我已经用下面的代码替换了包装器元素代码,以使其正常工作:
const Wrapper = styled.div`
scroll-snap-type: y mandatory;
max-height: 100vh;
overflow-y: scroll;
`;这解决了几个问题:
发布于 2021-06-16 02:22:10
我在使用scroll-snap时也遇到了一些问题。
对我来说,没有overflow-y: scroll就不能工作,试着把它放在你的包装器上。
https://stackoverflow.com/questions/54784885
复制相似问题