温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

web前端入门到实战:HTML定位和布局----float浮动

发布时间:2020-06-08 16:17:47 来源:网络 阅读:256 作者:前端向南 栏目:web开发

经过最近两年多的发展,小程序的地位也逐渐越来越高了,各个平台前赴后继做了自家的小程序平台,随着市场的需求越来愈多,uni-app 是一个使用 Vue.js 开发所有前端应用的框架,开发者编写一套代码,可发布到iOS、Android、H5、以及各种小程序(微信/支付宝/百度/头条/QQ/钉钉)等多个平台。即使不跨端,uni-app同时也是更好的小程序开发框架。

效果图

web前端入门到实战:HTML定位和布局----float浮动

web前端入门到实战:HTML定位和布局----float浮动

1、获取位置信息

在定位功能中,本程序用到腾讯地图的api 以及 腾讯天气的api接口,
需要到官网中注册开发者账号,通过注册后得到的appKey来请求我们需要的数据,详细注册步骤请自行度娘
由于需要用到定位功能,uniapp的getLocation方法获取到的是当前位置的坐标,然后对应腾讯地图具体城市

uni.getLocation({
    // #ifdef MP-WEIXIN
    type: 'wgs84',
    // #endif
    async success (res) {
        const {latitude, longitude} = res
        const result = await that.ajax({url: 'https://apis.map.qq.com/ws/geocoder/v1', data: {
            location: `${latitude},${longitude}`,
            key: ''
        }})
        let {province, city, district} = result.result.address_component
        that.getData(province, city, district)
    },
    fail(){
        uni.showModal({
          content: '检测到您没打开定位权限,是否去设置打开?',
          confirmText: "确认",
          cancelText: "取消",
          success: function (res) {
            if (res.confirm) {
              // #ifdef MP-WEIXIN
              wx.openSetting({
                success: (res) => {
                    that.getIn()
                }
              })
              // #endif
              // #ifdef MP-ALIPAY
              my.openSetting({
                 success: (res) => {
                    that.getIn()
                 }
              })
              // #endif
            }
          }
        });
    }
})
web前端开发学习Q-q-u-n: 784783012 ,分享学习的方法和需要注意的小细节,不停更新最新的教程和学习方法
(详细的前端项目实战教学视频,PDF)

2、查询天气

得到城市名后,再用城市名查询天气的接口,得到未来几天的天气预报。
天气接口使用腾讯天气接口api。
在小程序中使用前,要在小程序设置界面,开发设置中添加request合法域名。

methods: {
    async getData(province, city, district){
        const that = this
        const data = await that.ajax({url: 'https://wis.qq.com/weather/common', data: {
            source: 'xw',
            weather_type: 'observe|alarm|air|forecast_1h|forecast_24h|index|limit|tips|rise',
            province: province,
            city: city,
            county: district
        }})
        that.region = [province, city, district]
        if(data.status != 200){
            uni.showToast({
                title: result.message,
                icon: 'none'
            });
            return false;
        }
        if(!data.data.air.aqi_name){
            uni.showToast({
                title: '暂无该地区的天气信息',
                icon: 'none'
            });
            return false;
        }
        that.data = data.data
    }
}

3、小程序界面

由于没有什么审美,缺乏想象力,参考腾讯天气的界面来做的。功能十分简单,查看当前地区的天气和切换其他地区的天气,查看最近24小时的天气情况以及最近6天的天气情况,展示今天的农历时间。

4、插件使用

想快速完成小程序的搭建,里面的折线图采用的uchart.js,
因为可以兼容支付宝小程序和微信小程序,农历查询也是采用的插件calendar.js
折线图在支付宝小程序中会有模糊的问题,需要做兼容处理

<template>
<!-- #ifdef MP-ALIPAY -->
<canvas canvas-id="canvas" id="canvas" width="750" height="240"  class="canvas">
</canvas>
<!-- #endif -->
<!-- #ifdef MP-WEIXIN -->
<canvas canvas-id="canvas" id="canvas" class="canvas">
</canvas>
<!-- #endif -->
</template>

<script>
var wxCharts = require('../../utils/chart.js');
lineChart = new wxCharts({
    $this: this,
    canvasId: 'canvas',
    type: 'line',
    categories: ['', '', '', '', '' ,''],
    colors: ['#ffad35', '#4fc3f7'],
    background: '#fff',
    animation: true,
    series: [{
        name: '',
        data: that.max,
        format: function (val, name) {
            return val + '°';
        }
    }, {
        name: '',
        data: that.min,
        format: function (val, name) {
            return val + '°';
        }
    }],
    xAxis: {
        disableGrid: true,
        disabled: true,
        axisLine: false
    },
    yAxis: {
        max: Math.max.apply(Math, that.max) * 1 + 0.1,
        disabled: true,
        disableGrid: true,
    },
    legend:{ 
        show: false 
    },
    // #ifdef MP-ALIPAY
    pixelRatio: that.pixelRatio, // 解决支付宝模糊问题
    // #endif
    width: that.cWidth,
    height: that.cHeight
});
</script>
web前端开发学习Q-q-u-n: 784783012 ,分享学习的方法和需要注意的小细节,不停更新最新的教程和学习方法
(详细的前端项目实战教学视频,PDF)

微信小程序有城市选择组件,支付宝的没有可以直接使用的城市组件,uniapp官方介绍:支持安装 mpvue 组件,但npm方式不支持小程序自定义组件(如 wxml格式的vant-weapp),找到一款支付宝可以使用的城市插件:mpvue-citypicker,
城市选择组件

<template>
    <view class="txt-location" @tap="showCityPicker">
        <view class="icon"></view>
        <block v-if="region.length">{{region[0]}}{{region[1]}}{{region[2]}}</block>
        <block v-else>选择城市</block>
        <!-- #ifdef MP-WEIXIN -->
        <picker class="city" mode="region" @change="handleChange" :value="region">
            <view class="picker">
                当前选择:{{region[0]}},{{region[1]}},{{region[2]}}
            </view>
         </picker>
        <!-- #endif -->
    </view>
    <mpvue-city-picker ref="mpvueCityPicker" :pickerValueDefault="pickerValueDefault" @onConfirm="onConfirm"></mpvue-city-picker>
</template>

<script>
import mpvueCityPicker from 'mpvue-citypicker';
export default {
  data() {
    return {
      region: [],
      pickerValueDefault: [0, 0, 1]
    };
  },
  components: {
    mpvueCityPicker
  },
  methods: {
    showCityPicker() {
        // #ifdef MP-ALIPAY
        this.$refs.mpvueCityPicker.show()
        // #endif
    },
    onConfirm(e) {
        if(e.label){
            this.region = e.label.split('-')
            this.getData(this.region[0], this.region[1], this.region[2])
        }
    },
    handleChange(e) {
        this.region = e.detail.value
        this.getData(this.region[0], this.region[1], this.region[2])
    }
  }
};
</script>
web前端开发学习Q-q-u-n: 784783012 ,分享学习的方法和需要注意的小细节,不停更新最新的教程和学习方法
(详细的前端项目实战教学视频,PDF)
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI