使用uniapp做一个动画页面往下滑搜索框缓慢移动到顶部导航栏并将搜索框缩小覆盖原来的导航栏内容但是不覆盖左边的回到上一页面的按钮
您可以使用uni-app的动画组件和样式来实现这个效果。以下是一个实现该效果的示例代码:
- 在页面的
<template>标签中编写HTML结构:
<view>
<view class="navbar">
<image src="返回按钮图片路径" class="back-btn"></image>
<view class="search-bar" :style="searchBarStyle">
<input type="text" placeholder="搜索">
</view>
</view>
<view class="content">
<!-- 页面内容 -->
</view>
</view>
- 在页面的
<script>标签中添加数据和方法:
export default {
data() {
return {
searchBarStyle: '' // 搜索框样式
};
},
methods: {
// 监听页面滚动事件
onPageScroll(event) {
const scrollTop = event.scrollTop;
if (scrollTop > 100) { // 当页面滚动距离超过100时
this.searchBarStyle = 'position: fixed; top: 0; left: 0; width: 100%; height: 60px; background-color: #fff; z-index: 10; transform: scale(0.8) translate(10px, 10px);' // 设置搜索框样式
} else {
this.searchBarStyle = ''; // 恢复搜索框样式
}
}
}
}
- 在页面的
<style>标签中添加样式:
.navbar {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 60px;
background-color: #fff;
z-index: 10;
}
.back-btn {
position: absolute;
top: 50%;
left: 10px;
transform: translateY(-50%);
width: 30px;
height: 30px;
/* 添加返回按钮样式 */
}
.search-bar {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 80%;
height: 40px;
background-color: #f2f2f2;
border-radius: 20px;
padding: 5px;
/* 添加搜索框样式 */
}
.content {
margin-top: 60px; /* 保持页面内容不被导航栏遮挡 */
}
注意:在实际开发中,您需要根据自己的需求调整样式和动画效果。以上代码仅提供了一个简单的示例供参考
原文地址: https://www.cveoy.top/t/topic/iqVD 著作权归作者所有。请勿转载和采集!