【微信小程序】3|首页搜索框 | 我的咖啡店-综合实训

首页-搜索框-跳转

引言

在微信小程序中,首页的搜索框是用户交互的重要入口。本文将通过"我的咖啡店"小程序的首页搜索框实现,详细介绍如何在微信小程序中创建和处理搜索框的交互。

1. 搜索函数实现

onClickInput函数在用户点击搜索框时触发,显示加载提示并跳转到搜索页面。

javascript 复制代码
// pages/home/home.js
Page({
  onClickInput: function() {
    wx.showLoading({
      title: '努力加载中...',
    });
    wx.navigateTo({
      url: '/pages/search/search' // 请根据实际路径修改
    });
  },
  // ...其他函数
});

2. WXML 结构

home.wxml中,我们使用<van-search>组件创建搜索框,并使用<swiper>组件创建轮播图。

XML 复制代码
<!--pages/home/home.wxml-->
<view class="home">
  <!-- 搜索框开始 -->
  <van-search shape="round" background="#fff" placeholder="搜索咖啡店" bind:click-input="onClickInput">
  </van-search>
  <!-- 搜索框结束 -->

  <!-- 轮播图开始 -->
  <swiper indicator-dots indicator-active-color="#ff7173" autoplay interval="3000" circular>
    <swiper-item wx:for="{{bannerList}}" wx:key="index">
      <image src="{{item}}" referrerPolicy="no-referrer" class="b-img" />
    </swiper-item>
  </swiper>
  <!-- 轮播图结束 -->
</view>

结语

通过上述步骤,我们成功在"我的咖啡店"小程序的首页实现了搜索框功能。用户可以通过点击搜索框跳转到搜索页面,进行搜索操作。

完整代码

home.js

javascript 复制代码
// pages/home/home.js

Page({

  //页面的初始数据

  data: {

    // 轮播图数据

    bannerList: [

      'https://i-blog.csdnimg.cn/direct/420b00699f0643d88fd7bd549cba78a4.png',

      'https://i-blog.csdnimg.cn/direct/d41b0f66fc404023b4796cb0dd8bafaa.png',

      'https://i-blog.csdnimg.cn/direct/92f3b715f4d94723a6b9b1d941d16aa3.png',

      'https://i-blog.csdnimg.cn/direct/2c21368a3870417f9ca522b2c0975b5d.png'

    ]

  },



  // 搜索函数,点击搜索框时触发

  onClickInput: function () {

    // 加载提示

    wx.showLoading({

      title: '努力加载中...',

    });

    // 直接跳转到搜索页面

    wx.navigateTo({

      url: '/pages/search/search' // 请根据实际路径修改

    });

  },

})

home.wxml

XML 复制代码
<!--pages/home/home.wxml-->

<!-- div ==> view  img ==> image -->



<view class="home">

  <!-- 搜索框开始 -->

  <van-search shape="round" background="#fff" placeholder="{{defaultSearchHint}}" bind:click-input="onClickInput">

  </van-search>

  <!-- 搜索框结束 -->



  <!-- 轮播图开始 -->

  <swiper indicator-dots indicator-active-color="#ff7173" autoplay interval="3000" circular>

    <!-- wx:for="{{数据}}" wx:key  默认 index下标   item每一项 -->

    <swiper-item wx:for="{{bannerList}}" wx:key="index">

      <image src="{{item}}" referrerPolicy="no-referrer" class="b-img" />

    </swiper-item>

  </swiper>

  <!-- 轮播图结束 -->



</view>

展示

相关推荐
HuYi_code7 小时前
WeChat 小程序下载文件实现
微信小程序·uni-app
00后程序员张7 小时前
HTTPS 包 抓取与分析实战,从抓包到解密、故障定位与真机取证
网络协议·http·ios·小程序·https·uni-app·iphone
一匹电信狗8 小时前
【C++】C++风格的类型转换
服务器·开发语言·c++·leetcode·小程序·stl·visual studio
2501_915921438 小时前
iOS混淆与IPA加固实战手记,如何构建苹果应用防反编译体系
android·macos·ios·小程序·uni-app·cocoa·iphone
Q_Q51100828510 小时前
python+uniapp基于微信小程序的学院设备报修系统
spring boot·python·微信小程序·django·flask·uni-app
李慕婉学姐10 小时前
【开题答辩过程】以《自习室预约微信小程序》为例,不会开题答辩的可以进来看看
微信小程序·小程序
LB211211 小时前
苍穹外卖-购物车 前端修改(小程序主页与购物车模块显示不一致)
小程序
peachSoda711 小时前
自定义配置小程序tabbar逻辑思路
javascript·vue.js·微信小程序·小程序
ღ᭄ꦿ࿐Never say never꧂12 小时前
微信小程序 Button 组件 open-type 完全指南:从用户信息获取到客服分享的实战应用
spring boot·微信小程序·小程序·uni-app·vue3
2501_9151063220 小时前
iOS 26 APP 性能测试实战攻略:多工具组合辅助方案
android·macos·ios·小程序·uni-app·cocoa·iphone