如何打造可交互的 MIP 站点

本示例针对在 HTML 页面有通过数据驱动更新 DOM 节点需求的开发者

完整示例代码

其实执行到上一步我们已经完成了示例的全部开发步骤。但为了清晰和可读性考虑,我们把最终形态的示例代码展示给开发者。

index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<html> <head> <meta charset="utf-8"> <title>首页</title> <meta name="apple-touch-fullscreen" content="yes"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="format-detection" content="telephone=no"> <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no"> <link rel="stylesheet" href="https://c.mipcdn.com/static/v2/mip.css"> <style mip-custom> body { padding: 10px; } .mod { border: 1px solid rgba(0, 0, 0, 0.2); margin-bottom: 20px; } label { line-height: 40px; } input { border: 1px solid #666; width: 100px; height: 20px; padding: 6px; } ul { list-style-type: none; display: flex; } li { flex: 1 1 auto; background: rgba(0, 0, 0, 0.1); padding: 10px 0; text-align: center; border-left: 1px solid #bbb; } li:first-of-type { border: none; } .loading-tip { text-align: center; height: 100px; line-height: 100px; width: 100%; } </style> </head> <body> <mip-data> <script type="application/json"> { "no": 0, "imgList": [ "https://www.mipengine.org/static/img/sample_01.jpg", "https://www.mipengine.org/static/img/sample_02.jpg", "https://www.mipengine.org/static/img/sample_03.jpg" ], "tab": "娱乐", "loadingTip": "default" } </script> </mip-data> <div class="mod"> <label for="input"> 输入数字 0-2 以切换图片: <input id="input" type='text' on="change:MIP.setData({no:DOM.value})"> </label> <mip-img m-bind:src="imgList[no]" width="100%" height="300px"></mip-img> </div> <div class="mod"> <ul> <li on="tap:MIP.setData({tab: '娱乐'})">娱乐</li> <li on="tap:MIP.setData({tab: '体育'})">体育</li> <li on="tap:MIP.setData({tab: '新闻'})">新闻</li> </ul> <div m-text="loadingTip" class="loading-tip"></div> </div> <mip-script> console.log('watched') MIP.watch('tab', function (newVal) { MIP.setData({ loadingTip: `正在加载${newVal}频道的数据...` }) }) </mip-script> <script src="https://c.mipcdn.com/static/v2/mip.js"></script> <script src="https://c.mipcdn.com/static/v2/mip-script/mip-script.js"></script> </body> </html>