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

              
                #app
  .apptitle Vue.js Theater
  .movie(@wheel.prevent="wheel")
    .cards(:class="{isCartOpen: isCartOpen}")
      .card(v-for="movie in movies")
        .left
          // 記得style前有冒號,v-bind屬性綁定
          .cover(:style="bgcss(movie.cover)")
        .right
          h2 {{ movie.name }}
          h4 {{ movie.type }}
          p(v-html="movie.description")
          .price ${{ movie.price }}
          button.add(@click="addCart(movie, $event)") + Add to My Cart
  
  //- 如果沒有當下選取的電影就不要顯示
  .buybox(:style="bgcss(currentMovie.cover)", v-if="currentMovie")  
  
  .fixed-control(@click="isCartOpen=!isCartOpen")
    i.fa.fa-shopping-cart
    span {{ cart.length }}
  
  .control(:class="{isCartOpen: isCartOpen}")
    .panel
      h2 My Movie Cart
      ul
        li(v-for="(movie, mid) in cart")
          .thumbnail(:style="bgcss(movie.cover)")
          h3 {{ movie.name }}
            .price ${{ movie.price }}
            .remove(@click="cart.splice(mid, 1)") -
          
        li(v-if="cart.length==0")
          h3 Your cart is empty! :(
          
        hr
        h2 ${{ totalPrice }}
              
            
!

CSS

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

@mixin size($w, $h:$w)
  width: $w
  height: $h
  
@mixin flexCenter
  display: flex
  justify-content: center
  align-items: center
  
@mixin boxshadow
  box-shadow: 0px 5px 25px 5px rgba(black, 0.2)
  
@mixin fixedPos($l, $t)
  position: fixed
  left: $l
  top: $t

*
  vertical-align: center
  outline: none

html, body
  height: 100%
  margin: 0
  font-family: 'Roboto', sans-serif

#app
  +size(100%)
  background-color: #111
  display: flex  // 大小撐滿整個畫面
  overflow: hidden  // 讓捲軸消失

.apptitle
  +fixedPos(30px, 30px)
  color: white
  font-size: 30px
  font-weight: bold

.movie
  flex: 1  // Safari要多設定這個
.cards
  +flexCenter  // 整條左右排列
  height: 100%
  justify-content: left
  margin-left: 20vw
  margin-right: 20vw
  position: relative
  left: 0px
  transition: 0.5s, left 0s
  &.isCartOpen
    transform: scale(0.8)
  
.card
  margin: 60px
  +size(460px, auto)
  flex-shrink: 0  // 讓元素不被壓縮
  background-color: rgba(white, 0.9)
  border-radius: 5px
  padding: 20px
  padding-bottom: 0px
  +boxshadow
  transition: 0.5s
  color: #888
  cursor: pointer
  display: inline-flex  // 海報和說明左右排列
  .left
    flex: 1  // 比例關係
    // padding到0沒得縮,也無法用負的,小訣竅從margin下手
    margin-bottom: -15px  
    .cover
      +size(180px, 240px)
      border-radius: 5px
      +boxshadow
      transition: 0.5s
      position: relative
      top: -50px
  .right
    flex: 2
    padding: 20px
    padding-top: 10px
    h2
      margin: 0
      font-weight: bold
      font-size: 24px
      color: #444
    h4
      margin-top: 10px
      margin-bottom: 10px
      opacity: 0.8
      font-weight: normal
    p
      font-size: 13px
      line-height: 1.3
      text-align: justify  // 讓英文字在格子左右撐滿
      opacity: 0.8
      min-height: 5em  // 5倍字的高度
    .price
      display: inline-block
      margin-right: 20px
    button
      padding: 5px 10px
      background-color: #bbb
      color: rgba(white, 0.9)
      border-radius: 50px
      cursor: pointer
      transition: 0.5s
      &:hover
        color: white
        background-color: #f95e5e
  &:hover
    transform: translateY(-10px)
    .left .cover
      transform: translateY(-10px)
  
.fixed-control
  position: fixed
  right: 30px
  top: 30px
  color: white
  z-index: 1000
  opacity: 0.5
  cursor: pointer
  transition: 0.5s
  &:hover
    opacity: 1
  i
    font-size: 30px
    margin-right: 10px
    
.buybox
  +size(50px, 80px)
  background-color: #fff
  position: fixed
  right: 30px
  top: 30px
  opacity: 0
  
.control
  +size(100%)
  +fixedPos(0,0)
  +flexCenter
  color: #eee
  background-image: linear-gradient(10deg, #111 0%, #111 50%, transparent 100%)
  padding: 5vw
  box-sizing: border-box  // 不加會算入padding會歪掉
  opacity: 0
  pointer-events: none  // 隱藏時不接受滑鼠事件,才點得到Add
  transition: 0.5s
  &.isCartOpen
    opacity: 1
    pointer-events: initial
  .panel
    width: 70%
    transition: 0.5s
  ul
    padding: 0
    list-style: none
    li
      display: flex
      padding: 5px 10px
      margin-bottom: 5px
      margin-top: 5px
      cursor: pointer
      border-radius: 5px
      transition: 0.5s
      &:hover
        background-color: rgba(white, 0.1)
        transform: translateY(-10px)
      h3
        font-size: 17px
        font-weight: normal
        display: inline-block
        width: 100%
        opacity: 0.8
        .price
          float: right
        .remove
          display: inline-block
          padding: 4px 15px
          background-color: rgba(white, 0.3)
          border-radius: 50px
          font-size: 18px
          transition: 0.5s
          margin-left: 10px
          &:hover
            background-color: #ef4c4c
            color: black
      .thumbnail
        +size(50px, 70px)
        display: inline-block
        margin-right: 20px
              
            
!

JS

              
                var vm = new Vue({
  el: "#app",
  data: {
    movies: [],
    cart: [],
    currentMovie: null,
    isCartOpen: false
  },
  created() {
    let apiUrl = "https://awiclass.monoame.com/api/command.php?type=get&name=movies"
    // 用箭頭函式的目的是讓this成功指向vm本體,而非該函式本身
    axios.get(apiUrl).then(res=>{
      this.movies = res.data
    })
  },
  methods: {
    bgcss(url) {
      return {
        'background-image': 'url(' + url + ')',
        'background-position': 'center center',
        'background-size': 'cover'
      }
    },
    wheel(evt) {
      console.log(evt.deltaY)
      TweenMax.to(".cards", 0.8, {
        left: "+=" + evt.deltaY*2 + "px"
      })
    },
    addCart(movie, evt) {
      
      // 紀錄選擇的電影到currentMovie
      this.currentMovie = movie
      // 紀錄按鈕位置準備讓.buybox飛
      let target = evt.target
      // 在元件更新後,才執行動畫的更新
      this.$nextTick(()=>{
        
        TweenMax.from(".buybox", 0.8, {
          left: $(evt.target).offset().left,
          top: $(evt.target).offset().top,
          opacity: 1,
          ease: Power1.easeOut
        })

        setTimeout(()=>{
          this.cart.push(movie)
        }, 800)

        console.log(evt)
      })

    }
  },
  computed: {
    totalPrice() {
      return this.cart
                .map(movie=>movie.price)
                .reduce((total,p)=>total+p,0)  
    }
  },
  watch: {
    cart() {
      // 購物車縮放
      TweenMax.from(".fa-shopping-cart", 0.3, {
        scale: 0.5
      })
    }
  }
})
              
            
!
999px

Console