Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id='app'/>
              
            
!

CSS

              
                // *{
//   border: solid 1px black
// }
@mixin trans($t:0.5s , $tdelay:0s){
  transition: $t $tdelay;
}
@mixin flex_center{
  display:flex;
  justify-content:center;
  align-items:center;
}

@mixin size($w , $h:$w){
  width: $w;
  height: $h
}

html,body{
  @include size(100%);
  margin: 0;
  overflow:hidden;
  background-color: #1c1c1c;
}
#app{
  @include size(100%);
  @include flex_center;
}
.carouselContainer{
  @include size(1000px, auto);
  display: flex;
  justify-content: center;
}
.carouselArea{
  @include size(800px, auto);
  white-space: nowrap;
  position: relative;
  overflow:hidden;
  box-sizing:border-box;
  >.carouselPosts{
    position: relative;
    transition:0.5s;
  }
}
.carouselPostBox{
  @include size(100%);
  display:inline-block;
  box-sizing:border-box;
  >.cover{
    @include size(100%);
    @include trans;
    background-position:center,center;
    background-size:cover;
    &:hover{
       transform: scale(1.1);
    }
  }
}
.controlLeft , .controlRight {
  @include size(40px);
  @include flex_center;
  @include trans;
  border: solid 1px white;
  border-radius: 50%;
  color:white;
  font-size: 30px;
  cursor: pointer;
  position: absolute;
  top:50%;
  transform: translateY(-50%);
  &:hover{
     color:#1c1c1c;
     background-color: rgba(255,255,255,.8);
  }
}
.controlLeft{
  left: 1rem;
}
.controlRight{
  right: 1rem;
}



////// version 2
.contentBox{
  width:100%;
  height:100%;
  display: flex;
  align-items: center;
  background-color: #fff;
  box-sizing: border-box;
  padding: 15px;
}
.cardBox{
  display:flex;
  
  .imageStyle{
    width: 100px;
    height: 100px;
    border-radius: 5px;
    @include trans;
    background-position:center,center;
    background-size:cover;
    margin-right: 5px;
  }
  .fontBox{
    display:flex;
    flex-direction: column;
    width:calc(100% - 100px - 5px);
  }
  .titleStyle{
    font-size: 20px;
    line-height:20px;
    font-weight: bolder;
    white-space: pre-line;
    letter-spacing:1px;
    
  }
}

              
            
!

JS

              
                let imageArray = [
  {
    image:'https://picsum.photos/id/0/5616/3744',
    title:'學長歸來,MUST國際音樂創作營開箱文',
    time:'2019-10-26'
  },
  {
    image:'https://picsum.photos/id/10/2500/1667',
    title:'學姊歸來,爽爽開學去',
    time:'2019-10-26' 
  },
   {
    image:'https://picsum.photos/id/1000/5626/3635',
    title:'學弟歸來,開心吃東西去',
    time:'2019-10-26' 
  } ,
   {
    image:'https://picsum.photos/id/1003/1181/1772',
    title:'老師歸來,悲傷挨罵去',
    time:'2019-10-26' 
  }
]
class Container extends React.Component{
  renderChildrenView = (item ,index) =>{
    return (
      <div className='contentBox' key={index}>
        <div className='cardBox'>
          <div 
            style={{backgroundImage:`url(${item.image})`}} 
            className="imageStyle"/> 
          <div className='fontBox'>
            <p className='titleStyle'>{item.title}</p>
            <span>{item.time}</span>
          </div>

        </div>
        
     </div>
    )
  }
  render(){
    return(
      <React.Fragment>
         <Carousel 
          dataArray = {imageArray }
          autoplay={true}
          delay={10}
          carouselPostWidth={'400px'} 
          carouselPostHeight={150}
          carouselPostMargin={10}>
          {this.renderChildrenView}
          
         
          </Carousel>
      </React.Fragment>
    )
  }
}



class Carousel extends React.Component{
  state = {
    nowIndex:0,
  }
  componentDidMount(){
    if(this.props.autoplay){
      // this.autoPlay()
    }
  }
  componentWillUnmount(){
    clearInterval(this.timer)
  }
  autoPlay = () =>{
    this.timer = setInterval(()=>{
          this.changeImagePosition(2)
     },this.props.delay*1000);
  }

  conputedLeft = () =>{
    const {carouselPostWidth,carouselPostHeight,carouselPostMargin} = this.props
    const {nowIndex} = this.state
    console.log('nowIndex',nowIndex)
    let leftSpan = parseInt(`${-nowIndex * parseInt(carouselPostWidth)}`)
    return{
      left: carouselPostWidth.toString().match(/[%vw]/) != null ? 
      `calc(${leftSpan}% - ${carouselPostMargin*2*nowIndex}px)`: 
      `${leftSpan - (carouselPostMargin*2*nowIndex)}px`
    }
  }
  
  changeImagePosition = (index) =>{
     const {dataArray , block} = this.props
     const {nowIndex} = this.state
     // (1 + 1 + 3) % 3
     this.setState({
       nowIndex : (nowIndex + index +  dataArray.length) % dataArray.length
     })
  }
  
  render(){
    const {dataArray,carouselPostMargin,carouselPostWidth,carouselPostHeight} = this.props
    return(
      <div className='carouselContainer'>
        <div className="carouselArea">
          <div style={this.conputedLeft()} className="carouselPosts">
            {dataArray.map((imgaeUrl,index)=>{
              return(
               <div 
                 key={imgaeUrl} 
                  style={{
                    width:carouselPostWidth, 
                    height:carouselPostHeight,  
                    margin:`0px ${carouselPostMargin}px `,...this.props.carouselPostStyle}} 
                  className="carouselPostBox">
                  {this.props.children(imgaeUrl,index)}
               </div>
              )
            })}
        </div>
      </div>
      
        <div onClick={()=>this.changeImagePosition(-2)} className="controlLeft"><i className="fa fa-angle-left"  /></div>
        <div onClick={()=>this.changeImagePosition(2)} className="controlRight"><i className="fa fa-angle-right" /></div>
      </div>
    )
  }
}




ReactDOM.render(<Container/>,document.getElementById("app"))


/*
  dataArray 數據陣列
  autoplay={true} 自動撥放
  delay={10} 延遲多九
  carouselPostWidth={'50vw'} 每塊寬度
  carouselPostHeight={100} 每塊高度
  carouselPostStyle={{boxShadow:`1px 10px 15px #fff`}} 樣式
  carouselPostMargin={10}/>,document.getElementById("app")

*/
              
            
!
999px

Console