后浪云百度小程序教程:开发 .swan 文件

  • 开发 .swan 文件
    • 基础数据绑定
    • 循环
    • 条件
    • 事件
      • 事件处理
      • 事件对象
      • dataset
      • touches

    开发 .swan 文件

    这部分是每个智能小程序页面的展现模板,类似于 Web 开发中的 HTML,SWAN 模板中使用的标签均为 SWAN 组件规定的标签。

    代码示例

    • SWAN
     
     
     
    1. <view s-for="item in items" class="single-item" bind:tap="oneItemClick" bind:touchstart="oneItemTouchStart" bind:touchmove="oneItemTouchmove" bind:touchcancel="oneItemTouchcancel" bind:touchend="oneItemTouchEnd">
    2. <image src="{{item.imgsrc}}" class="single-img"></image>
    3. <view class="single-text-area">
    4. <text class="single-title">
    5. {{item.title}}
    6. </text>
    7. <view s-if="{{item.tags}}" class="tag-area">
    8. <text s-for="tag in item.tags" class="{{tag.className}}">
    9. {{tag.content}}
    10. </text>
    11. </view>
    12. </view>
    13. </view>
    14. <view class="view-more" bind:tap="loadMore">
    15. <text>点击加载更多</text>
    16. </view>

    标签可以拥有属性,需要注意的是,swan 中的属性是大小写敏感的,也就是说 class 和 Class 在 swan 中是不同的属性。

    代码示例

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • SWAN
     
     
     
    1. <text class="wrap">hello world</text>
    2. <text Class="wrap">hello world</text>

    一个文件夹可以有两个 swan 文件。

    代码示例

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    基础数据绑定

    代码示例

    • SWAN
    • JS
     
     
     
    1. <!-- xxx.swan -->
    2. <view>
    3. Hello My {{ name }}
    4. </view>
     
     
     
    1. // xxx.js
    2. Page({
    3. data: {
    4. name: 'SWAN'
    5. }
    6. });

    循环

    开发者可以通过在元素上添加s-for指令,来渲染一个列表。

    代码示例

    • SWAN
    • JS
     
     
     
    1. <view>
    2. <view s-for="p in persons">
    3. {{p.name}}
    4. </view>
    5. </view>
     
     
     
    1. Page({
    2. data: {
    3. persons: [
    4. {name: 'superman'},
    5. {name: 'spiderman'}
    6. ]
    7. }
    8. });

    条件

    开发者可以通过在元素上添加s-if指令,来在视图层进行逻辑判断:

    代码示例

    • SWAN
    • JS
     
     
     
    1. <view s-if="is4G">4G</view>
    2. <view s-elif="isWifi">Wifi</view>
    3. <view s-else>Other</view>
     
     
     
    1. Page({
    2. data: {
    3. is4G: true,
    4. isWifi: false
    5. }
    6. });

    事件

    详情请参考事件处理。

    事件处理

    开发者可以使用bind: + 事件名来进行事件绑定

    代码示例

    • SWAN
    • JS
     
     
     
    1. <view class="view-more" bind:tap="loadMore">
    2. 点击加载更多
    3. </view>
     
     
     
    1. Page({
    2. loadMore: function () {
    3. console.log('加载更多被点击');
    4. }
    5. });

    目前支持的事件类型有:

    类型 触发条件
    touchstart 手指触摸开始
    touchmove 手指触摸后进行移动
    touchend 手指触摸结束
    touchcancel 手指触摸动作被打断,如来电提醒等
    tap 手指触摸后马上离开动作

    事件对象

    当开发者绑定方法到事件,事件触发时,SWAN 会给触发的方法传递事件对象,事件对象因事件不同而不同,目前基础的事件对象结构为:

    • JSON
     
     
     
    1. {
    2. "changedTouches": [{
    3. "clientX": 194,
    4. "clientY": 401,
    5. "force": 0,
    6. "identifier": 0,
    7. "pageX": 194,
    8. "pageY": 401,
    9. "x": null,
    10. "y": null
    11. }],
    12. // 事件触发的属性集合
    13. "currentTarget": {
    14. "dataset": {},
    15. "id": "_9be18",
    16. "offsetLeft": 31,
    17. "offsetTop": 377
    18. },
    19. "detail": {
    20. "x": 194,
    21. "y": 401
    22. },
    23. "target": {
    24. "dataset": {},
    25. "id": "_9be18",
    26. "offsetLeft": 31,
    27. "offsetTop": 377
    28. },
    29. "timeStamp": 10303373,
    30. "touches": [],
    31. // 事件类型
    32. "type": "tap"
    33. }

    dataset

    开发者可以在组件中自定义数据,并在事件发生时,由 SWAN 所在事件对象中,传递给绑定函数。

    代码示例

    • SWAN
    • JS
     
     
     
    1. <view data-swan="1" bind:tap="viewtap">dataset-test</view>
     
     
     
    1. Page({
    2. viewtap: function (event) {
    3. // 输出1
    4. console.log('value is:', event.currentTarget.dataset.swan);
    5. }
    6. });

    属性值也可以动态的去改变,有所不同的是,属性值必须被包裹在双引号中, 且引号可加可不加

    代码示例

    • SWAN
    • JS
     
     
     
    1. <view data-swan={{test}} bind:tap="viewtap">dataset-test</view>
    2. // 同<view data-swan="{{test}}" bind:tap="viewtap">dataset-test</view>
     
     
     
    1. Page({
    2. data: {
    3. test: 1
    4. },
    5. viewtap: function (event) {
    6. // 输出1
    7. console.log('value is:', event.currentTarget.dataset.swan);
    8. }
    9. });

    需要注意的是变量名是大小写敏感的,也就是说 test 和 Test 是两个不同的变量。

    代码示例

    • SWAN
    • JS
     
     
     
    1. <view data-swan="{{test}}" bind:tap="viewtap">dataset-test</view>
     
     
     
    1. Page({
    2. data: {
    3. test: 1,
    4. Test: 2
    5. },
    6. viewtap: function (event) {
    7. // 输出1
    8. console.log('value is:', event.currentTarget.dataset.swan);
    9. }
    10. });

    touches

    开发者在接收到触摸类事件后,在事件对象上,可以接收到当前停留在屏幕上的触摸点。

    Touch 对象

    属性 类型 描述
    pageX , pageY Number 距离文档左上角的距离,横向为 X,纵向为 Y
    clientX , clientY Number 距离屏幕视口左上角距离,横向为 X,纵向为 Y

    代码示例

    • SWAN
    • JS
     
     
     
    1. <view bind:touchstart="viewtouchstart">viewtouchstart</view>
     
     
     
    1. Page({
    2. viewtouchstart: function (event) {
    3. console.log('value is:', event.touches);
    4. // 输出 clientX: 44,clientX: 47,pageX: 44, pageY: 47
    5. }
    6. });
    THE END