Home > Web Front-end > uni-app > How to make uniapp webview pop-up window

How to make uniapp webview pop-up window

PHPz
Release: 2023-04-23 09:42:57
Original
1530 people have browsed it

With the popularity of mobile web applications, Webview pop-up windows have become one of the common requirements in mobile web application development. As an excellent cross-platform development framework, Uniapp also provides Webview-related components and APIs, allowing developers to easily implement the function of Webview pop-up windows.

This article will focus on the method and specific steps of how to use Webview to implement pop-up windows in Uniapp.

  1. Create Webview component

First, create a Webview component in Uniapp. In Uniapp, we can use the webview component to display web pages.

Code example:

<template>
  <view class="container">
    <web-view :src="url" :style="webViewStyle" @message="onMessage"></web-view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      webViewStyle: {
        height: `${uni.upx2px(500)}px`
      },
      url: 'https://www.example.com'
    }
  },
  methods: {
    onMessage(e) {
      //  接收来自webview组件发来的数据
      console.log(e.detail.data)
    }
  }
}
</script>
Copy after login
  1. Introduce the Webview component into the pop-up window component

Next, we need to introduce the Webview component into the pop-up window component. In this case, we will create a bottom pop-up component that will be displayed when the user clicks on other components.

Code example:

<template>
  <view>
    <!-- 遮罩 -->
    <view class="mask" v-show="visible" @click="onClose"></view>

    <!-- 底部弹窗 -->
    <view class="popup" :class="{ show: visible }">
      <webview :src="url" :style="webViewStyle"></webview>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      visible: false, // 是否展示底部弹窗
      webViewStyle: {
        height: `${uni.upx2px(500)}px`
      },
      url: 'https://www.example.com'
    }
  },
  methods: {
    // 打开底部弹窗
    open() {
      this.visible = true;
    },
    // 关闭底部弹窗
    onClose() {
      this.visible = false;
    }
  }
}
</script>

<style>
.popup {
  position: fixed;
  bottom: 0;
  width: 100%;
  height: auto;
  background-color: #fff;
  z-index: 1000;
  transform: translateY(100%);
  transition: transform .3s;
}

.popup.show {
  transform: translateY(0);
}

.mask {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: .6;
  background-color: #000;
  z-index: 999;
  transition: opacity .3s;
}

.mask.show {
  opacity: 1;
}
</style>
Copy after login
  1. Trigger pop-up window

Finally, we need to listen to click events in other components, and when the user clicks, call the pop-up window The open method of the window component is used to display the pop-up window.

Code example:

<template>
  <view>
    <view class="button" @click="showPopup">显示弹窗</view>
    <popup ref="popup"></popup>
  </view>
</template>

<script>
    import Popup from './components/popup'

    export default {
        components: {
            Popup
        },
        methods: {
            // 显示弹窗
            showPopup() {
                this.$refs.popup.open()
            }
        }
    }
</script>
Copy after login

Okay, now you know the method and specific steps of using Uniapp to implement Webview pop-up window. I believe that everyone can freely modify and expand it according to their own project needs and preferences to achieve richer functions. I hope this article can be helpful to everyone, thank you for reading!

The above is the detailed content of How to make uniapp webview pop-up window. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template