微信小程序的遍历

const query = wx.createSelectorQuery().in(this)

query.selectAll('.distanceKm').boundingClientRect()

query.exec(function (res) {

console.log(res)

})

第02个小程序:遍历画笔(FlipThroughTheBrushes.cs) using System; using System.Reflection; using System.Windows; using System.Windows.Input; using System.Windows.Media; namespace Chapter02 { public class FlipThroughTheBrushes : Window { int index = 0; //索引值 PropertyInfo[] props; //属性数组 [STAThread] public static void Main() { Application app = new Application(); app.Run(new FlipThroughTheBrushes()); } public FlipThroughTheBrushes() { //取得Brushes类的成员,放在props中 props = typeof(Brushes).GetProperties(BindingFlags.Public | BindingFlags.Static); SetTitleAndBackground(); } //鼠标按下时 protected override void OnKeyDown(KeyEventArgs args) { if (args.Key == Key.Down || args.Key == Key.Up) { //Key.Down时index-1,Key.UP时index+1 index += args.Key == Key.Up ? 1 : props.Length - 1; //index对属性数组长度取余,防止索引越界 index %= props.Length; SetTitleAndBackground(); } base.OnKeyDown(args); } //设置背景 void SetTitleAndBackground() { Title = "Flip Through the Brushes - " + props[index].Name; Background = (Brush)props[index].GetValue(null, null); //设置背景画笔 } } } GetProperties()函数用于返回对象数组,参数是限制Brushes公开和静态的属性。其实这里可以不需要这样的限制,因为Brushes的属性本来全部都是public和static。 props[index].Name返回第index个属性的名称;props[index].GetValue(null, null)返回实际的SolidColorBrush对象,第一个参数是属性所在的对象,因为Brushes是一个静态属性,没有对应的对象,传入null;第二个参数只有在属性是索引器是才有必要。
微信小程序中实现图片的遍历展示,可以通过列表渲染的方式完成。具体来说,可以使用 `wx:for` 指令对数据数组进行循环,从而动态生成图片列表。以下是一个完整的实现示例: ### 数据准备 首先,在页面的 `data` 中定义一个图片路径数组,例如: ```javascript Page({ data: { images: [ 'https://example.com/image1.jpg', 'https://example.com/image2.jpg', 'https://example.com/image3.jpg' ] } }); ``` ### 页面结构 在 WXML 文件中,使用 `wx:for` 遍历 `images` 数组,并为每张图片绑定点击事件: ```html <view wx:for="{{images}}" wx:key="index"> <image src="{{item}}" bindtap="getSrc" data-src="{{item}}"></image> </view> ``` 上述代码中,`wx:key="index"` 是为了优化列表渲染性能,`bindtap` 绑定了一个点击事件,并通过 `data-src` 属性传递了图片的 `src` 值。 ### 点击事件处理 在页面的 JavaScript 文件中,定义 `getSrc` 函数用于获取点击的图片 `src`: ```javascript Page({ data: { images: [ 'https://example.com/image1.jpg', 'https://example.com/image2.jpg', 'https://example.com/image3.jpg' ] }, getSrc: function(e) { const src = e.currentTarget.dataset.src; console.log('点击的图片 src 是:', src); } }); ``` 通过 `e.currentTarget.dataset.src` 可以获取到点击图片的 `src` 值,并在控制台输出。 ### 轮播图实现 如果需要实现轮播图效果,可以通过定时器结合数组索引的更新来完成。以下是一个简单的轮播图实现示例: ```javascript Page({ data: { currentIndex: 0, images: [ 'https://example.com/image1.jpg', 'https://example.com/image2.jpg', 'https://example.com/image3.jpg' ] }, onLoad: function() { this.startCarousel(); }, startCarousel: function() { setInterval(() => { const { currentIndex, images } = this.data; const nextIndex = (currentIndex + 1) % images.length; this.setData({ currentIndex: nextIndex }); }, 3000); // 每3秒切换一张图片 } }); ``` 在 WXML 文件中,根据 `currentIndex` 显示当前图片: ```html <image src="{{images[currentIndex]}}" mode="aspectFill"></image> ``` ### 样式优化 为了提升用户体验,可以为图片添加一些样式,例如设置固定尺寸和过渡效果: ```css image { width: 100%; height: 300rpx; transition: all 0.5s ease; } ``` ### 总结 通过上述步骤,可以实现在微信小程序遍历展示图片列表,并且支持点击获取图片的 `src` 和轮播图功能。整个实现过程结合了数据绑定、事件处理和样式优化,能够满足常见的图片展示需求。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值