Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

全宽的上方阴影 #8

Open
gaoryrt opened this issue Nov 26, 2018 · 4 comments
Open

全宽的上方阴影 #8

gaoryrt opened this issue Nov 26, 2018 · 4 comments
Labels
effect construct of some effect

Comments

@gaoryrt
Copy link
Member

gaoryrt commented Nov 26, 2018

image

👆要达到这个效果,底 bar 在可滚动内容上有一个上阴影

放大一点:
image

@gaoryrt
Copy link
Member Author

gaoryrt commented Nov 26, 2018

box-shadow 的语法:

.sub-bar {
  /* offset-x | offset-y | blur-radius | spread-radius | color */
  box-shadow: 2px 2px 2px 1px rgba(0, 0, 0, 0.2);
}

这样通过缩小和平移,是可以写出来「上方阴影」的:
image

.sub-bar {
  box-shadow: 0 -5px 5px -5px rgba(0, 0, 0, 0.3)
}

image

.sub-bar {
  box-shadow: 0 -2.5px 5px -2.5px rgba(0, 0, 0, .3)
}

但是由于 blur 拥有圆角,上方全宽的阴影会在左右溢出,左右不溢出的上方不能均匀全宽

image

把第二个例子的阴影颜色加深后如上图。单靠 box-shadow,不能做到单边全宽并且其他边不溢出。

@gaoryrt
Copy link
Member Author

gaoryrt commented Nov 26, 2018

1. overflow: hidden

这个很简单,就是在父元素上把其他边的阴影给隐藏掉

.container {
	position: relative;
	overflow-x: hidden;
	overflow-y: auto;
}

@gaoryrt
Copy link
Member Author

gaoryrt commented Nov 26, 2018

2. clip-path

同样是把其他边隐藏掉,使用 clip-path 不需要 container

.sub-bar {
	 box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
     clip-path: polygon(0 -5px, 100% -5px, 100% 100%, 0% 100%)
}

顺便推荐一个在线生成器:http://bennettfeely.com/clippy/

@gaoryrt
Copy link
Member Author

gaoryrt commented Nov 26, 2018

3. 阴影元素

不用 sub-bar 自带的阴影了,自己写一个

.shadow {
    position: absolute;
    bottom: 20px;
    left: 0;
    width: 100%;
    height: 5px;
    background: linear-gradient(transparent, rgba(0, 0, 0, .15));
  }

@gaoryrt gaoryrt added the effect construct of some effect label Nov 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effect construct of some effect
Projects
None yet
Development

No branches or pull requests

1 participant