"使用uniapp做一个动画,页面往下滑时,搜索框缓慢移动到顶部导航栏并将搜索框缩小,覆盖原来的导航栏内容,但是不覆盖左边的回到上一页面的按钮内容:可以使用uni-app的动画组件<animation>来实现搜索框的动画效果。\n\n首先,在页面的<template>中添加搜索框和导航栏的代码:\n\nhtml\n<template>\n <view>\n <view class="search-box" :style="{ height: searchBarHeight + 'px' }">\n <!-- 搜索框内容 -->\n </view>\n <view class="nav-bar">\n <!-- 导航栏内容 -->\n <view class="back-btn">返回</view>\n </view>\n </view>\n</template>\n\n\n然后,在页面的<script>中添加动画效果的代码:\n\njavascript\n<script>\nexport default {\n data() {\n return {\n searchBarHeight: 100, // 初始搜索框高度\n };\n },\n methods: {\n handleScroll(e) {\n // 获取页面滚动距离\n const scrollTop = e.detail.scrollTop;\n\n // 计算搜索框新的高度\n const newSearchBarHeight = 100 - scrollTop;\n\n // 更新搜索框高度\n this.searchBarHeight = newSearchBarHeight;\n },\n },\n};\n</script>\n\n\n最后,在页面的<style>中添加样式:\n\ncss\n<style>\n.search-box {\n position: fixed;\n top: 0;\n left: 0;\n right: 0;\n background-color: #fff;\n transition: height 0.3s;\n}\n\n.nav-bar {\n height: 100px;\n background-color: #ccc;\n position: fixed;\n top: 0;\n left: 0;\n right: 0;\n}\n\n.back-btn {\n position: absolute;\n left: 20px;\n top: 50%;\n transform: translateY(-50%);\n}\n</style>\n\n\n这样,当页面滚动时,搜索框的高度会缓慢减小,从而实现搜索框往导航栏移动的效果。同时,左边的回到上一页面的按钮不会被搜索框覆盖。\n


原文地址: https://www.cveoy.top/t/topic/p8JO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录