Home > Web Front-end > uni-app > uniapp closes app horizontal screen

uniapp closes app horizontal screen

WBOY
Release: 2023-05-26 16:27:07
Original
2337 people have browsed it

With the popularity of smart phones, people increasingly rely on mobile phones for various operations and entertainment. However, in some cases, such as when playing a game or viewing a photo gallery, a user may prefer to view mobile content in landscape orientation. In this case, many applications can automatically switch to landscape mode based on the user's needs. However, some developers may not have considered these needs or provided users with the option to turn off horizontal screen. In this article, we will introduce how to turn off the horizontal screen function in uniapp.

In uniapp, you can turn on the horizontal screen by using the following code:

uni.setScreenOrientation({
    orientation: 'landscape',
    success: function () {
        console.log('设置屏幕方向为横屏');
    }
})
Copy after login

Similarly, we can turn off the horizontal screen by using the following code:

uni.setScreenOrientation({
    orientation: 'portrait',
    success: function () {
        console.log('设置屏幕方向为竖屏');
    }
})
Copy after login

As you can see, Both functions use the uni.setScreenOrientation() method to set the screen orientation. Using this method, we can easily switch the screen orientation in uniapp.

So, how to implement this function in your own application? It's actually very simple. You just need to provide users with an option to turn off horizontal screen. The following are some implementation methods:

  1. Create a button

You can add a button to the page and bind it to a function that closes the horizontal screen. For example:

<template>
  <view>
    <button @click="closeOrientation">关闭横屏</button>
  </view>
</template>

<script>
  export default {
    methods: {
      closeOrientation() {
        uni.setScreenOrientation({
          orientation: 'portrait',
          success: function () {
            console.log('设置屏幕方向为竖屏');
          }
        })
      }
    }
  }
</script>
Copy after login

When the user clicks this button, uniapp will call the closeOrientation() function to close the horizontal screen.

  1. Create a switch

In addition to creating a button, we can also create a switch that allows the user to switch the screen orientation at any time. The following is an example of a switch implemented using the switch component:

<template>
  <view>
    <switch v-model="isOrientationOn" @change="onChange"></switch>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        isOrientationOn: true,
      }
    },
    methods: {
      onChange(value) {
        if (value === false) {
          uni.setScreenOrientation({
            orientation: 'portrait',
            success: function () {
              console.log('设置屏幕方向为竖屏');
            }
          })
        } else {
          uni.setScreenOrientation({
            orientation: 'landscape',
            success: function () {
              console.log('设置屏幕方向为横屏');
            }
          })
        }
      }
    }
  }
</script>
Copy after login

When the user switches the switch, uniapp calls the onChange() function and passes the value of the switch to it. In the function, we can determine the status of the switch and then set the orientation of the screen.

  1. Create a menu item

If the application has a menu function, we can also add an option to the menu to turn off the screen orientation. The following is an example of a menu option implemented using the uni-popup-menu component:

<template>
  <view>
    <uni-popup-menu>
      <uni-popup-menu-item @click="closeOrientation">关闭横屏</uni-popup-menu-item>
    </uni-popup-menu>
  </view>
</template>

<script>
  export default {
    methods: {
      closeOrientation() {
        uni.setScreenOrientation({
          orientation: 'portrait',
          success: function () {
            console.log('设置屏幕方向为竖屏');
          }
        })
      }
    }
  }
</script>
Copy after login

When the user clicks the menu option, uniapp will call the closeOrientation() function to turn off the screen orientation.

Summary

By using the uni.setScreenOrientation() method, we can easily implement the function of turning off the horizontal screen in uniapp. Whether through buttons, switches or menu options, it can bring users a convenient and friendly application experience. When developing uniapp applications, be sure to pay attention to the user experience and provide users with more choices and freedom.

The above is the detailed content of uniapp closes app horizontal screen. 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