Angular2 - Components - articleItem

偷取了这个 CSS 效果做了一个组件

原设计地址:
http://codepen.io/balapa/pen/vLzmmM

  • 原设计尺寸位置都是固定的。css 做了一些改动,并且动画改动了下,没有做椭圆轨迹的移动。以后看能否加上
  • 添加了基本的文字 overflow 的处理
  • 和 Read More 按钮
  • 箭头引用的是 awesome Font 的 lib

这个组件接受 3 个 inputs

  • height
  • width
  • article 对象
    都有默认值

效果图:


源码地址 https://github.com/dreambo8563/CSS_Learning/tree/master/articleItem

基本使用 <animate height="600px" width="800px" [article]="article"></animate>

import {
Component,
OnInit,
OnDestroy,
ViewChild,
ElementRef,
Renderer,
Input,
Optional,
Self,
Attribute
} from "angular2/core"

@Component({
selector: "animate",
templateUrl: "./app/components/articleItem.html",
styleUrls: ["./app/components/articleItem.css"]
})
export class ArticleItem implements OnInit, OnDestroy {
@ViewChild("content")
content: ElementRef
@ViewChild("header")
header: ElementRef

@Input("article")
article: { title: string, content: string }

_article = {
title: `This is a title`,
content: `This is the main content. In this area,
the header will be hidden. `
}
_height: string = "400px"
_width: string = "400px"

constructor(
@Optional()
@Self()
@Attribute("height")
height: string,
@Optional()
@Self()
@Attribute("width")
width: string
) {
this._height = height || this._height
this._width = width || this._width
}

ngOnInit() {
this._article = this.article || this._article
}
ngOnDestroy() {
console.log("ngOnDestroy")
}

toggle() {
this.header.nativeElement.classList.toggle("active")
this.content.nativeElement.classList.toggle("active")
}
}

inputs 值的检查放到 OnInit 里,因为在构造的时候还是 undefined

@import url(https://fonts.googleapis.com/css?family=Roboto:400, 300, 500);

::-webkit-scrollbar {
display: none;
}

::-moz-scrollbar {
display: none;
}

.cardRoot {
/*height: 800px;
width: 400px;
margin-left: 500px;*/
position: relative;
border-style: solid;
border-color: #f9b24f;
}

header {
height: 100%;
width: 100%;
pointer-events: none;
}

header .title {
position: absolute;
top: 40%;
left: 10%;
color: white;
font-size: 40px;
line-height: 40px;
transition: 0.3s all ease;
z-index: 999;
font-family: Roboto;
font-weight: 300;
text-transform: capitalize;
right: 10%;
word-wrap: break-word;
bottom: 10%;
overflow: hidden;
}

header .header-bg {
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15);
background: #ec4590;
transition: 0.3s all ease;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}

header.active .title {
font-size: 20px;
line-height: 30px;
top: 4%;
text-shadow: 10px 5px 2px #0f0c0f;
transition: 0.3s all ease 0.15s;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}

header.active .header-bg {
height: 20%;
background: #e11873;
transition: 0.3s all ease 0.15s;
}

header.active .cta-ctr {
top: calc(20% - 30px);
bottom: auto;
-webkit-transform: rotate(0deg);
-moz-transform: rotate(0deg);
-ms-transform: rotate(0deg);
-o-transform: rotate(0deg);
transform: rotate(0deg);
transition: 0.5s all ease 0.2s;
}

header.active .cta .fa {
-webkit-transform: rotate(180deg);
-moz-transform: rotate(180deg);
-ms-transform: rotate(180deg);
-o-transform: rotate(180deg);
transform: rotate(180deg);
transition: 0.5s all ease 0.2s;
}

header .cta-ctr {
position: absolute;
right: 10%;
top: calc(100% - 30px);
-webkit-transform: rotate(-90deg);
-moz-transform: rotate(-90deg);
-ms-transform: rotate(-90deg);
-o-transform: rotate(-90deg);
transform: rotate(-90deg);
transition: 0.3s all ease;
}

header .cta {
width: 60px;
height: 60px;
background: #f9b24f;
border-radius: 50%;
cursor: pointer;
transition: 0.3s all ease;
pointer-events: auto;
position: relative;
box-shadow: 0 8px 12px 1px rgba(0, 0, 0, 0.15);
}

header .cta .fa {
border-radius: 50%;
position: absolute;
color: white;
font-size: 20px;
right: 20px;
bottom: 20px;
-webkit-transform: rotate(90deg);
-moz-transform: rotate(90deg);
-ms-transform: rotate(90deg);
-o-transform: rotate(90deg);
transform: rotate(90deg);
transition: 0.3s all ease;
}

header .cta:hover {
background-color: #fabd68;
}

.content-ctr {
width: 100%;
height: 100%;
background: white;
overflow: scroll;
position: relative;
}

.content-ctr.active .header-content-ctr {
height: 0%;
transition: 0.3s height ease 0.45s;
}

.content-ctr.active .header-content-ctr .title {
transition: 0.3s all ease;
opacity: 0;
-webkit-transform: translateY(-20px);
-moz-transform: translateY(-20px);
-ms-transform: translateY(-20px);
-o-transform: translateY(-20px);
transform: translateY(-20px);
}

.content-ctr.active .main-content-ctr {
height: 100%;
overflow: scroll;
}

.content-ctr.active .main-content-ctr .par {
transition: 0.3s all ease 0.7s;
-webkit-transform: translateY(0px);
-moz-transform: translateY(0px);
-ms-transform: translateY(0px);
-o-transform: translateY(0px);
transform: translateY(0px);
opacity: 1;
}

.content-ctr .title {
font-family: Roboto;
margin-bottom: 30px;
font-size: 34px;
color: #e61875;
transition: 0.3s all ease;
}

.content-ctr .par {
font-family: Roboto;
font-size: 18px;
line-height: 1.5;
font-weight: 300;
color: #777;
}

.content-ctr .header-content-ctr {
position: relative;
height: 100%;
overflow: hidden;
transition: 0.3s height ease;
}

.content-ctr .header-content-ctr .title {
transition: 0.3s all ease 0.15s;
}

.content-ctr .header-content {
position: absolute;
right: 0;
bottom: 110px;
left: 80px;
}

.content-ctr .main-content-ctr {
position: relative;
min-height: 0%;
transition: 0.3s height ease;
overflow: hidden;
}

.content-ctr .main-content-ctr .title,
.content-ctr .main-content-ctr .par {
opacity: 0;
-webkit-transform: translateY(20px);
-moz-transform: translateY(20px);
-ms-transform: translateY(20px);
-o-transform: translateY(20px);
transform: translateY(20px);
}

.content-ctr .main-content {
position: relative;
top: 35%;
/* right: 10%; */
left: 0;
margin-right: 10%;
margin-left: 10%;
height: 60%;
overflow: hidden;
}

.read-more {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
text-align: center;
margin: 0;
padding: 4px 0;
/* "transparent" only works here because == rgba(0,0,0,0) */
background-image: linear-gradient(to bottom, transparent, #fabd68);
transition: 0.8s all ease;
}

.read-more a.button {
color: #e61875;
text-decoration: none;
}

.read-more:hover {
font-size: 24px;
}
<div class="cardRoot" [style.width]="_width" [style.height]="_height">
<div #content class='content-ctr'>
<div class='main-content-ctr'>
<div class='main-content'>
<div class='par'>{{_article.content}} </div>
<p class="read-more"><a href="#" class="button">Read More</a></p>
</div>
</div>
</div>
<header #header>
<div class='header-bg'></div>
<div class='header-content'>
<div class='title'>{{_article.title}}</div>
</div>
<div class='cta-ctr'>
<div class='cta' (click)="toggle()">
<i class="fa fa-arrow-up"></i>
</div>
</div>
</header>
</div>