taro实现小程序端瀑布流

微信小程序暂时未发现如何获取dom,但能获取节点的信息

taro端可通过ref获取到节点,但无法通过节点获取节点的相关信息

效果图:
在这里插入图片描述在这里插入图片描述

代码示例:

import Taro from '@tarojs/taro'
import {View,Text,Image} from '@tarojs/components'
import React,{Component} from 'react'

import './index.less'
/**
 * https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2209999056,1217794382&fm=26&gp=0.jpg
 * https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2418077635,996250637&fm=26&gp=0.jpg
 * https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2261944737,731173482&fm=26&gp=0.jpg
 * https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1874647665,1205912684&fm=26&gp=0.jpg
 * https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3152590946,2826023176&fm=26&gp=0.jpg
 * https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3975309943,373981693&fm=26&gp=0.jpg
 */

class Recommend extends Component{

    state={
        arr:[
            'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2209999056,1217794382&fm=26&gp=0.jpg',
            'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2418077635,996250637&fm=26&gp=0.jpg',
            'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2261944737,731173482&fm=26&gp=0.jpg',
            'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1874647665,1205912684&fm=26&gp=0.jpg',
            'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3152590946,2826023176&fm=26&gp=0.jpg',
            'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3975309943,373981693&fm=26&gp=0.jpg'
        ],
        initHeights:[]
    }
	
	//制造瀑布流单个元素
     _madeImage(str,index)
    {


        return <View className='.r-o'>
                <Image  src={str} />
                {
                    index%2==0?<View style='height:100px'>哇啦啦啦</View>:<View style='height:50px'>啦啦</View>
                }
            </View>
        
    }
    componentDidMount()
    {
 		//200毫秒后获取节点信息,避免取值为null
	    setTimeout(()=>{
	        const query = Taro.createSelectorQuery()
	        query.selectAll('.r-o').boundingClientRect((res)=>{
	            // 第二个参数是通过ref获取到的瀑布流的每一个元素
	            this._getHeight(res,this.f.childNodes);
	        }).exec()
	    },200)
	
	    }
	    _getHeight=(nodes,childNodes)=>
	    {
			//盛放每行高度的数组
	        let heights=[];
	        nodes.forEach((item,index) => {
	        	//前两个为基准
	            if(index<2)
	            {
	                heights.push(item.height);
	            }else{
	            	//查找最低高度然后放置最后改变最低高度过程
	                    let min=Math.min.apply(null,heights);
	                    let currentIndex=heights.indexOf(min);
	                    let x=nodes[currentIndex].left;
	                    let y=min;
	
	                    childNodes[index].style.position='absolute';
	                    
	                    childNodes[index].style.left=x-10+'px';
	                    childNodes[index].style.top=y+'px';
	
	                    heights[currentIndex]=min+item.height;
	
	                    
	            }
	        });
    }

    render()
    {
        return(

            <View className='r-c'>
                <Text className='r-t' id='f'>为你推荐</Text>
                <View className='r-i' id='ri' ref={(v)=>this.f=v} name='ww'>
                    {
                        this.state.arr.map((item,index)=>{
                           return this._madeImage(item,index);

                        })
                    }
                </View>
            </View>
        )
    }
}

export default Recommend

less:

.r-c{
    margin-top: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;    


    .r-t{
        width: 100%;
        font-weight: bold;
        margin: 20px;
        text-align: left;
        >Text{
            margin-left: 25px;
        }
    }
    .r-i{
        position: relative;
        .r-o{
            float: left;
            width: 320px;
            margin: 0 5px;
            border:solid 1px #ccc;
            >Image{
                width: 100%;
            }
            .r-info{
                display: flex;
                flex-direction: column;
                padding: 20px 20px;
                >Text{
                    line-height: 50px;
                }
                >Text:nth-child(1)
                {
                    font-weight: bold;
                }
                >Text:nth-child(2)
                {
                    color:#ccc;
                    font-size: 25px;
                }
                >Text:nth-child(3)
                {
                    color:#F93841;
                }
            }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
您提到的小程序悬浮搜索框可以通过在小程序页面中使用 `navigator` 组件和 `fixed` 定位来实现。具体的实现步骤如下: 1. 在小程序页面的 `json` 文件中添加 `navigator` 组件,设置 `url` 属性为搜索页面的路径,设置 `open-type` 属性为 `navigate`,设置 `hover-class` 属性为 `none`,并在组件内部添加一个 `input` 输入框。 ```json { "usingComponents": { "navigator": "/miniprogram_npm/@tarojs/components/dist/taro-components/taro-components.esm" }, "navigationBarTitleText": "首页", "navigationBarBackgroundColor": "#fff", "navigationBarTextStyle": "black", "enablePullDownRefresh": true, "backgroundTextStyle": "dark", "backgroundColor": "#F5F5F5", "usingComponents": {}, "component": true, "disableScroll": true, "navigationStyle": "custom", "window": { "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#fff", "navigationBarTitleText": "WeChat", "navigationBarTextStyle": "black" }, "usingComponents": { "navigator": "/miniprogram_npm/@tarojs/components/dist/taro-components/taro-components.esm" }, "disableScroll": true, "navigationBarTitleText": "首页", "enablePullDownRefresh": true, "backgroundTextStyle": "dark", "backgroundColor": "#F5F5F5", "usingComponents": {} } ``` 2. 在小程序页面的 `scss` 文件中添加以下样式,设置 `navigator` 组件的位置和样式。 ```scss .navigator { position: fixed; left: 0; top: 0; right: 0; height: 40px; background-color: #fff; z-index: 999; display: flex; justify-content: center; align-items: center; padding: 0 10px; } .navigator input { width: 100%; height: 100%; border: none; outline: none; background-color: transparent; font-size: 14px; color: #333; padding: 0 10px; margin: 0; } ``` 3. 在小程序页面的 `js` 文件中添加以下代码,判断当前页面是否是搜索页面,如果是,则隐藏 `navigator` 组件;否则显示 `navigator` 组件。 ```js import Taro, { Component } from '@tarojs/taro' import { View, Input } from '@tarojs/components' class Index extends Component { config = { navigationBarTitleText: '首页' } state = { isSearchPage: false } onShow() { const pages = Taro.getCurrentPages() const currentPage = pages[pages.length - 1] const isSearchPage = currentPage.route === 'pages/search/search' this.setState({ isSearchPage }) } render() { const { isSearchPage } = this.state return ( <View className='index'> {!isSearchPage && ( <navigator className='navigator' url='/pages/search/search' open-type='navigate' hover-class='none'> <Input className='input' placeholder='请输入搜索内容' /> </navigator> )} <View className='content'>这是首页</View> </View> ) } } export default Index ``` 通过以上步骤,即可实现小程序悬浮搜索框的效果。需要注意的是,由于小程序的一些限制,`navigator` 组件在 iOS 系统下可能会出现样式问题,可以通过在 `app.js` 文件中设置 `window` 对象的 `disableScroll` 属性为 `true` 来解决。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值