无法使用react-sticky

时间:2018-09-05 20:54:29

标签: reactjs header sticky react-sticky

我正在开发一个网站,所以我不想粘贴标题部分。我正在使用https://www.npmjs.com/package/react-sticky

中的reactreact-sticky lib开发

这是我使用sticky的文件。

import React from 'React'
import { StickyContainer, Sticky } from 'react-sticky'

class main extends React.Component {
  render () {
    return (
      <StickyContainer>
        <Sticky>
          {({style}) => (
            <components.common.header style={style}/>
          )}
        </Sticky>
        <components.common..... />
        <components.common..... />
        <components.common..... />
        <components.common.footer />
      </StickyContainer>
    )
  }
}

export default main

这是我的webpack.config.js

const path = require('path')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const merge = require('webpack-merge')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const env = require('./src/env.js')
const TARGET = process.env.npm_lifecycle_event
process.env.BABEL_ENV = TARGET

const PATHS = {
  app: path.join(__dirname, 'src'),
  build: path.join(__dirname, 'build'),
  assets: path.join(__dirname, 'assets')
}

const common = {
  entry: [PATHS.app],
  module: {
    rules: [
      {
        test: /\.json$/,
        loader: 'json-loader'
      },
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        include: PATHS.app,
        loaders: ['babel-loader']
      },
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, { loader: 'css-loader', options: { modules: true } }],
        include: /flexboxgrid/
      },
      {
        test: /\.scss$/,
        exclude: /flexboxgrid/,
        use: [
          MiniCssExtractPlugin.loader,
          { loader: 'css-loader', options: { modules: true } },
          'sass-loader'
        ]
      },
      {
        test: /\.jpe?g$|\.gif$|\.png$/i,
        loader: 'url-loader?name=[name].[ext]'
      },
      {
        test: /\.otf$|\.eot$|\.svg$|\.ttf|\.woff|\.woff2$/,
        loader: 'url-loader?name=[name].[ext]'
      }
    ]
  },
  resolve: {
    extensions: ['.js', '.jsx'],
    modules: ['node_modules', path.resolve(__dirname, './node_modules')],
    mainFields: ['browser', 'web', 'browserify', 'main', 'style']
  }
}

if (TARGET === 'start' || !TARGET) {
  module.exports = merge(common, {
    module: {
      rules: [
        {
          test: /\.jsx?$/,
          use: ['source-map-loader'],
          enforce: 'pre'
        }
      ]
    },
    devtool: 'inline-source-map',
    devServer: {
      contentBase: PATHS.build,
      historyApiFallback: true,
      hot: true,
      inline: true,
      progress: true,
      stats: 'errors-only',
      https: true,
      host: env.host,
      port: env.port,
      overlay: {
        errors: true
      },
      watchOptions: {
        watch: true
      }
    },
    plugins: [
      new MiniCssExtractPlugin({ filename: 'assets/style.css' }),
      new webpack.HotModuleReplacementPlugin(),
      new HtmlWebpackPlugin({
        template: PATHS.app + '/index.html',
        inject: 'body'
      })
    ],
    output: {
      path: PATHS.build,
      filename: 'bundle.js',
      publicPath: '/'
    }
  })
}

if (TARGET === 'production') {
  module.exports = merge(common, {
    plugins: [
      new MiniCssExtractPlugin({ filename: 'style.css' }),
      new webpack.optimize.OccurenceOrderPlugin(),
      new webpack.DefinePlugin({
        'process.env': {
          NODE_ENV: "'production'"
        }
      })
    ],
    output: {
      path: '/build',
      filename: 'bundle.js'
    }
  })
}

我没有从控制台收到任何错误,问题是页眉没有停留在顶部。

有人可以帮助我吗?

谢谢!

编辑

经过更多测试后,我发现style为空。然后,当我更改屏幕尺寸时,它就变得可以工作了。

有一种方法可以强制此屏幕更新?

1 个答案:

答案 0 :(得分:0)

好的,我自己解决了,让我分享我所做的事情。

首先,要制作sticky,您不需要为此提供一个库。

您只需要在css中添加以下行

left: 0;
top: 0;
position: fixed;
transform: translateZ(0);

然后,您需要为其他组件使用z-index。

这样做可以在没有任何框架的情况下实现sticky

以后我要为自己一个问题:

这可以在所有设备上使用吗?