微信小程序弹窗

article/2025/9/21 10:00:15

微信小程序自定义底部、顶部、中间、左边、右边弹窗
简单的微信小程序弹窗功能,具体实现过程,请浏览代码。
点击事件
顶部弹出窗图例:
页面顶部弹出
中间弹出窗图例:
中部弹窗
底部弹出窗图例:
底部弹窗
左边弹出窗图例:
左边弹出
右边弹出窗图例:
右边弹出

  1. wxml代码
<button type="primary" bindtap="openPopup" data-index="0">打开顶部弹窗</button>
<button type="primary" bindtap="openPopup" data-index="1">打开中间弹窗</button>
<button type="primary" bindtap="openPopup" data-index="2">打开底部弹窗</button>
<button type="primary" bindtap="openPopup" data-index="3">打开左边弹窗</button>
<button type="primary" bindtap="openPopup" data-index="4">打开右边弹窗</button>
<!-- 顶部弹窗 -->
<view class="popup-box" wx:if="{{showIndex=='0'}}" bindtap="closePopup"></view> 
<view class="info-top" wx:if="{{showIndex=='0'}}"><view class="row-info"><view><text class="line left-line"></text><text>《登高》</text><text class="line right-line"></text></view><view class="row-author">杜甫 〔唐代〕</view><view>风急天高猿啸哀,渚清沙白鸟飞回。</view><view>无边落木萧萧下,不尽长江滚滚来。</view><view>万里悲秋常作客,百年多病独登台。</view><view>艰难苦恨繁霜鬓,潦倒新停浊酒杯。</view></view><view class="row-btn"><view class="left-btn" bindtap="closePopup">取消</view><view class="right-btn">确认</view></view>
</view>
<!-- 中间弹窗 -->
<view class="popup-box" wx:if="{{showIndex=='1'}}" bindtap="closePopup"></view> 
<view class="info-center" style="top:{{height*0.3}}px;height:{{height*0.4}}px;"
wx:if="{{showIndex=='1'}}"><view><view class="row-info"><view><text class="line left-line"></text><text>《登高》</text><text class="line right-line"></text></view><view class="row-author">杜甫 〔唐代〕</view><view>风急天高猿啸哀,渚清沙白鸟飞回。</view><view>无边落木萧萧下,不尽长江滚滚来。</view><view>万里悲秋常作客,百年多病独登台。</view><view>艰难苦恨繁霜鬓,潦倒新停浊酒杯。</view></view><view class="row-btn"><view class="left-btn" bindtap="closePopup">取消</view><view class="right-btn">确认</view></view></view>
</view>
<!-- 底部弹窗 -->
<view class="popup-box" wx:if="{{showIndex=='2'}}" bindtap="closePopup"></view> 
<view class="info-bottom" wx:if="{{showIndex=='2'}}"><view class="row-info"><view><text class="line left-line"></text><text>《登高》</text><text class="line right-line"></text></view><view class="row-author">杜甫 〔唐代〕</view><view>风急天高猿啸哀,渚清沙白鸟飞回。</view><view>无边落木萧萧下,不尽长江滚滚来。</view><view>万里悲秋常作客,百年多病独登台。</view><view>艰难苦恨繁霜鬓,潦倒新停浊酒杯。</view></view><view class="row-btn"><view class="left-btn" bindtap="closePopup">取消</view><view class="right-btn">确认</view></view>
</view><!-- 左边弹窗 -->
<view class="popup-box" wx:if="{{showIndex=='3'}}" bindtap="closePopup"></view> 
<view class="info-left" wx:if="{{showIndex=='3'}}"><view class="row-info"><view><text class="line left-line"></text><text>《登高》</text><text class="line right-line"></text></view><view class="row-author">杜甫 〔唐代〕</view><view>风急天高猿啸哀,</view><view>渚清沙白鸟飞回。</view><view>无边落木萧萧下,</view><view>不尽长江滚滚来。</view><view>万里悲秋常作客,</view><view>百年多病独登台。</view><view>艰难苦恨繁霜鬓,</view><view>潦倒新停浊酒杯。</view></view><view class="row-btn fixed"><view class="left-btn" bindtap="closePopup">取消</view><view class="right-btn">确认</view></view>
</view>
<!-- 右边弹窗 -->
<view class="popup-box" wx:if="{{showIndex=='4'}}" bindtap="closePopup"></view> 
<view class="info-right" wx:if="{{showIndex=='4'}}"><view class="row-info"><view><text class="line left-line"></text><text>《登高》</text><text class="line right-line"></text></view><view class="row-author">杜甫 〔唐代〕</view><view>风急天高猿啸哀,</view><view>渚清沙白鸟飞回。</view><view>无边落木萧萧下,</view><view>不尽长江滚滚来。</view><view>万里悲秋常作客,</view><view>百年多病独登台。</view><view>艰难苦恨繁霜鬓,</view><view>潦倒新停浊酒杯。</view></view><view class="row-btn fixed"><view class="left-btn" bindtap="closePopup">取消</view><view class="right-btn">确认</view></view>
</view>
  1. wxss代码
/* 蒙层 */
.popup-box{position: absolute;z-index: 99;top: 0;background-color: rgba(0, 0, 0, 0.5);width: 100%;height: 100%;
}
/* 上 */
.info-top{position: fixed;z-index: 999;width: 100%;top: 0;background-color: white;border-bottom-left-radius: 5rpx;border-bottom-right-radius: 5rpx;
}
/* 中 */
.info-center{position: fixed;z-index: 999;background-color: white;display: flex;align-items: center;justify-content: center;border-radius: 10rpx;width: 90%;margin-left: 5%;margin-right: 5%;
}
/* 下 */
.info-bottom{position: fixed;z-index: 999;width: 100%;bottom: 0;background-color: white;border-top-left-radius: 10rpx;border-top-right-radius: 10rpx;
}
/* 左 */
.info-left{position: fixed;z-index: 999;width: 50%;height: 100%;top: 0;background-color: white;border-top-right-radius: 10rpx;border-bottom-right-radius: 10rpx;
}
/* 右 */
.info-right{position: fixed;z-index: 999;width: 50%;height: 100%;right: 0;top: 0;background-color: white;border-top-left-radius: 10rpx;border-bottom-left-radius: 10rpx;
}
/* 自定义内容(根据自己需求更改,可删除) */
button{margin: 15rpx 0;
}
.row-info{display: flex;flex-direction: column;align-items: center;margin: 15rpx;font-size: 32rpx;
}
.row-info view{padding: 10rpx 0;
}
.row-info view:first-child{display: flex;flex-direction: row;align-items: center;
}
.line{width: 100rpx;height: 1rpx;
}
.left-line{background-image: linear-gradient(to left,orange,white);
}
.right-line{background-image: linear-gradient(to right,orange,white);
}
.row-author{font-size: 24rpx;color: gray;
}
.row-btn{display: flex;flex-direction: row;align-items: center;border-top: 1rpx dashed #f1f1f1;
}
.row-btn view{flex: 1;text-align: center;margin: 20rpx 10%;padding: 12rpx 0;font-size: 32rpx;border-radius: 10rpx;
}
.left-btn{background-color: #f1f1f1;color: #33ccff;
}
.right-btn{background-color: #33ccff;color: white;
}
.fixed{position: fixed;bottom: 0;width: 50%;
}
  1. js代码
Page({/*** 页面的初始数据*/data: {showIndex:null,//打开弹窗的对应下标height:'',//屏幕高度},// 打开弹窗openPopup(e){var index = e.currentTarget.dataset.index;this.setData({showIndex:index})},//关闭弹窗closePopup(){this.setData({showIndex:null})},/*** 生命周期函数--监听页面初次渲染完成*/onReady() {var that = this;// 动态获取屏幕高度wx.getSystemInfo({success: (result) => {that.setData({height: result.windowHeight});},})},
})

代码简洁,类型齐全。本人才疏学浅,代码仅供参考,如有问题,请多多指教,酸Q。


http://chatgpt.dhexx.cn/article/iNh4Ra5m.shtml

相关文章

微信小程序弹窗提示怎么写

第一种&#xff1a;弹出提示框&#xff0c;可以选择确定或者取消。 代码&#xff1a; wx.showModal({ title: 提示, content: 这是一个模态弹窗, success: function (res) { if (res.confirm) {//这里是点击了确定以后 console.log(用户点击…

微信小程序的弹窗提示

第一种&#xff1a;弹出提示框&#xff0c;用户可以选择确定或者取消&#xff0c;且都带有回调。 wx.showModal({title: 提示,content: 这是一个模态弹窗,success: function (res) {if (res.confirm) { //这里是点击了确定以后console.log(用户点击确定)} else { //这里是点击了…

CSS中水平居中、垂直居中和水平同时垂直居中

1.水平居中 2.垂直居中 3.水平并垂直居中 flex布局用到父元素上&#xff0c;绝对定位用到子元素上。

HTML/CSS:图片居中(水平居中和垂直居中)

css图片居中分css图片水平居中和垂直居中两种情况&#xff0c;有时候还需要图片同时水平垂直居中。 css图片水平居中 1.利用margin: 0 auto实现图片水平居中 利用margin: 0 auto实现图片居中就是在图片上加上css样式margin: 0 auto 如下&#xff1a; <div style"text-…

前端|如何让一个元素水平居中/垂直居中?

不知道你们是否有这样的体验&#xff0c;水平居中在前端中经常使用&#xff0c;但是&#xff01;&#xff01;&#xff01;每次一用就各种不起作用。各种justify-content,align-item,text-align,margin&#xff0c;经过种种尝试之后&#xff0c;终于能够居中了&#xff0c;但下…

微信小程序元素水平居中或垂直居中

.wxml<view class"father"> <view class"children1">子元素1</view> <view class"children2">子元素2</view> <view class"children3">子元素3</view> </view>.wxss.father{display:…

【CSS】元素居中总结-水平居中、垂直居中、水平垂直居中

【CSS】元素居中 一、 水平居中1.行内元素水平居中&#xff08;1&#xff09;text-align 2.块级元素水平居中2.1 margin&#xff08;1&#xff09;margin 2.2布局&#xff08;1&#xff09;flex justify-content&#xff08;推荐&#xff09;&#xff08;2&#xff09; flexmar…

关于照片(img)的水平居中和垂直居中

本文主要是讲述照片&#xff08;img&#xff09;的水平居中和垂直居中&#xff0c;但是其他元素的水平居中和垂直居中也可借鉴此文。 水平居中&#xff1a; 1.将img元素设置成块级元素 img {display: block;margin: 0 auto;} 2.flex布局 .box1 {width: 100px;height: 100p…

margin 实现水平居中,垂直居中

首先了解下&#xff0c;margin的auto属性的作用是用来分配剩余空间&#xff0c;所以对于有剩余空间的元素才有效哦&#xff08;块及元素&#xff09;。比如图片设置margin: 0 auto是无效的&#xff0c;因为图片是内联元素&#xff0c;不是占一整行&#xff0c;没有剩余空间。 1…

让div在屏幕中居中(水平居中+垂直居中)的几种方法

这里是修真院前端小课堂&#xff0c;本篇分析的主题是 【让div在屏幕中居中&#xff08;水平居中垂直居中&#xff09;的几种方法】 水平居中方法: 1.inline&#xff0c;inline-block元素的水平居中&#xff0c;在父级块级元素中设置text-align:center; 2.确定宽度的块级元素…

CSS布局对齐方式--水平居中、垂直居中、水平垂直居中

前言&#xff1a;在网页布局中&#xff0c;经常遇到需要使元素居中对齐的时候&#xff0c;居中对齐的方式有&#xff1a;水平居中、垂直居中和水平垂直居中。这次&#xff0c;借此回顾总结一下&#xff0c;并在此记录下相关内容。 一、水平居中&#xff1a; &#xff08;1&…

CSS水平居中+垂直居中+水平/垂直居中的方法总结

目录 水平居中 行内元素 块级元素 方案一&#xff1a;(分宽度定不定两种情况) 方案二&#xff1a;使用定位属性 方案三&#xff1a;使用flexbox布局实现&#xff08;宽度定不定都可以&#xff09; 垂直居中 单行的行内元素 多行的行内元素 块级元素 水平垂直居中 已…

HTML/CSS常见的几种水平居中、垂直居中、水平垂直居中方法

HTML/CSS常见的几种水平居中、垂直居中、水平居中方式 一、水平居中 1.第一种方式 在css中使用text-align和display属性 <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; ch…

CSS居中——水平居中、垂直居中方法

水平居中 1、行内或类行内元素&#xff08;如文本、链接&#xff09; 在块级父元素中用CSS样式实现行内元素水平居中&#xff0c;只需要设置&#xff1a;text-align: center; 这种方法可以让 inline / inline-block / inline-table / flex 等类型的元素实现居中。 效果图&a…

CSS实现水平居中与垂直居中

CSS实现水平居中与垂直居中 一、水平居中1. 必备知识1.1 text-align 属性1.2 display 属性1.3 margin 属性1.4 position 属性1.5 transform 属性 2. 实现方式2.1 内联元素居中2.2 块级元素居中2.2.1 margin: 0 auto2.2.2 text-align display2.2.3 position transform 二、垂直…

CSS基础知识(五):水平居中和垂直居中

文章目录 一、居中二、水平居中1. 行内元素水平居中2. 块级元素水平居中2.1 使用margin: 0 auto2.2 使用position2.3 使用flex 三、垂直居中1. 行内元素1.1 单行行内元素垂直居中1.2 多行行内元素垂直居中 2. 块级元素2.1 使用position2.2 使用flex 一、居中 行内元素&#xff…

字符串匹配算法之KMP算法(图例详解)

字符串匹配算法之KMP算法&#xff08;图例详解&#xff09; 1.字符串匹配算法及暴力算法1.1 简介1.2 示例题目 2.KMP算法&#xff08;Knuth-Morris-Pratt algorith&#xff09;2.1 朴素算法的缺点2.2 KMP算法2.2.1 KMP算法中的前缀算法2.2.1.1 前缀函数pi的定义2.2.1.2 前缀函数…

字符串匹配算法(KMP)

问题&#xff1a; 给你两个字符串 s 和 pat &#xff0c;请你在 s 字符串中找出 pat 字符串出现的第一个位置&#xff08;下标从 0 开始&#xff09;&#xff0c;如果不存在则返回-1。 1.暴力匹配算法。 暴力匹配算法较好理解&#xff0c;其大致原理如图&#xff1a; 当D和E不…

字符串匹配算法KMP详细解释——深入理解

1. 前言 字符串匹配是一个经典算法问题&#xff0c;展开来讲各类问题多达几十种&#xff0c;有名称的算法也不下三十种&#xff0c;所以需要深入学习的东西有很多。这次我们来探讨一个最简单的问题&#xff0c;假设现在随机输入一个长度为m的主串T&#xff0c;另外输入一个长度…

几种常见的字符串匹配算法

转自http://www.360doc.com/content/14/0325/15/15064667_363609292.shtml 1. 朴素算法 朴素算法是最简单的字符串匹配算法&#xff0c;也是人们接触得最多的字符串匹配算法。代码一看就懂&#xff0c;在此不在赘述。 #include<stdio.h> #include<string.h> voi…