vue/cli的学习七 事件操作效果切换

正常显示 中间的导航正常显示  

下拉以后 滚动一段距离以后 吸顶效果

 

1页面滚动的事件写在mounted中  拉动的距离大于 轮播图的高度以后  吸顶效果  title显示

 mounted() {
    //处理dom操作和事件绑定
    window.onscroll = () => {
      //滚动的事件中得到滚动的距离
      let scrollTop = document.documentElement.scrollTop;

      let bannerHeigth = this.$refs.myswiper.$el.offsetHeight;
      if (scrollTop >= bannerHeigth) {
        this.titleFlag = true;
        this.navclass="nav1";
      } else {
        this.titleFlag = false;
        this.navclass="nav";
      }
    };
  },

2:数据处理地方

 

data() {
    return {
      navclass:"nav",
      titleFlag: false,
      com: "nowplaying",
      selectedIndex: 1,
      bannerList: [], //轮播图的数据
    };
  },

完成的页面代码如下

<template>
  <div class="home">
    <swiper ref="myswiper" :bannerlist="bannerList"></swiper>
    <div :class="navclass">
      <div class="nav-title" v-show="titleFlag">电影</div>
      <div class="nav-content">
        <div
          class="nav-item"
          :class="{ selected: selectedIndex == 1 }"
          @click="changeIndex(1)"
        >
          正在热映
        </div>
        <div
          class="nav-item"
          :class="{ selected: selectedIndex != 1 }"
          @click="changeIndex(2)"
        >
          即将上映
        </div>
      </div>
    </div>
    <div class="content">
      <component :is="com"></component>
    </div>
  </div>
</template>
<style scoped>
.nav {
  width: 100%;
  height: 80px;
}
.nav1{
   width: 100%;
   height: 80px;
   top:0;
   position: sticky;
   background-color: white;
}
.nav-title {
  width: 100%;
  height: 40px;
  text-align: center;
  line-height: 40px;
}
.nav-content {
  width: 100%;
  height: 40px;
  display: flex;
}
.nav-item {
  flex: 1;
  text-align: center;
  line-height: 40px;
}
.nav-item.selected {
  color: #ff5f16;
}
</style>
<script>
import axios from "axios";
import swiper from "../components/home/Swiper";
import comingsoon from "../components/home/ComingSoon";
import nowplaying from "../components/home/NowPlaying";
export default {
  data() {
    return {
      navclass:"nav",
      titleFlag: false,
      com: "nowplaying",
      selectedIndex: 1,
      bannerList: [], //轮播图的数据
    };
  },
  methods: {
    changeIndex(index) {
      this.selectedIndex = index;
      if (index == 1) {
        //千万别写=
        this.com = "nowplaying";
      } else {
        this.com = "comingsoon";
      }
    },
  },
  components: {
    swiper,
    comingsoon,
    nowplaying,
  },
  created() {
    axios({
      url: "http://192.168.61.174:6789/bannerlist",
    }).then((res) => {
      console.log(res.data);
      this.bannerList = res.data;
    });
  },
  mounted() {
    //处理dom操作和事件绑定
    window.onscroll = () => {
      //滚动的事件中得到滚动的距离
      let scrollTop = document.documentElement.scrollTop;

      let bannerHeigth = this.$refs.myswiper.$el.offsetHeight;
      if (scrollTop >= bannerHeigth) {
        this.titleFlag = true;
        this.navclass="nav1";
      } else {
        this.titleFlag = false;
        this.navclass="nav";
      }
    };
  },
};
</script>

 

npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving: @vue/cli-plugin-vuex@4.4.6 npm ERR! Found: @vue/cli-service@5.0.8 npm ERR! node_modules/@vue/cli-service npm ERR! dev @vue/cli-service@"^5.0.8" from the root project npm ERR! peer @vue/cli-service@"^3.0.0 || ^4.0.0 || ^5.0.0-0" from @vue/cli-plugin-babel@5.0.8 npm ERR! node_modules/@vue/cli-plugin-babel npm ERR! dev @vue/cli-plugin-babel@"^5.0.8" from the root project npm ERR! 3 more (@vue/cli-plugin-pwa, @vue/cli-plugin-router, @vue/cli-plugin-vuex) npm ERR! npm ERR! Could not resolve dependency: npm ERR! peer @vue/cli-service@"^3.0.0 || ^4.0.0-0" from @vue/cli-plugin-vuex@4.4.6 npm ERR! node_modules/@vue/cli-plugin-vuex npm ERR! dev @vue/cli-plugin-vuex@"~4.4.0" from the root project npm ERR! npm ERR! Conflicting peer dependency: @vue/cli-service@4.5.19 npm ERR! node_modules/@vue/cli-service npm ERR! peer @vue/cli-service@"^3.0.0 || ^4.0.0-0" from @vue/cli-plugin-vuex@4.4.6 npm ERR! node_modules/@vue/cli-plugin-vuex npm ERR! dev @vue/cli-plugin-vuex@"~4.4.0" from the root project npm ERR! npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution. npm ERR! npm ERR! npm ERR! For a full report see: npm ERR! C:\Users\35514\AppData\Local\npm-cache\_logs\2023-06-08T07_30_06_817Z-eresolve-report.txt npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\35514\AppData\Local\npm-cache\_logs\2023-06-08T07_30_06_817Z-debug-0.log
06-09
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值