Mobile App list point-through event processing method

Source: Internet
Author: User

About the point through the incident here no longer repeat, if not clear can search the Internet, or see the small match of this article.

This is when you are doing the mobile side, when the list is sliding, the point of the problem encountered. The cause of this problem is because in the transition, the transition effect of each phone is not the same, some better, but in some low-end machine, the transition appears a little card, so the transition effect is removed, so is the direct routing switch. "Specific incident specific analysis, may I have encountered the problem is not suitable for you, here just posted out to share"

First look at the following two pictures:


Clicking on the button on the list page will switch to the next page, but each entry on the next page will be clickable, triggering the popup window of the next page, in fact we do not want to display the pop-up window directly, but wait for the user to click.

You may have written the following piece of code on the list page in your project:

Render(){    return(<UL ClassName="List">            {                 List.Map((l,Index= {                    return(<Li Key={ ' list${Index}` }OnClick={()= dosomething()}>                            { ' Item${Index}` }                        </li>                    )                })              }</ul>);}

A click event is bound to each item in a list, but when clicked, it switches to the next page. At that time, the mobile click event will have a 300ms delay, so after switching the page, the browser will again determine the behavior of the click, if the next page has a trigger click on the element, this time triggered the next page click Behavior.

So, you might do this, onClick change events into events onTouchEnd

<li     key={`list${index}`}     onTouchStart={=>event.preventDefault}    onTouchEnd={=>doSomething}>    {`item${index}`}</li>

However, each time you swipe, you also trigger the onTouchEnd event, so each time you swipe you will click into the next page. So you want to, add an onTouchStart event, and then stop the default event, and found that the slide is not moving.

So for several commonly used methods to solve the point-through event, I think of several solutions:

Scenario One: Simulate tap events yourself

The approximate code is as follows:

varList= Document.Queryselector(' #list ');varDragState= {};List.AddEventListener(' Touchstart ', function(event){    varTouch= Event.touches[0];    DragState.StartTime = New Date();    DragState.Startleft = Touch.PageX;    DragState.Starttop = Touch.Pagey;    DragState.Starttopabsolute = Touch.ClientY;});List.AddEventListener(' Touchmove ', function(event){    varTouch= Event.touches[0];    DragState.Currentleft = Touch.PageX;    DragState.Currenttop = Touch.Pagey;    DragState.Currenttopabsolute = Touch.ClientY;});List.AddEventListener(' Touchend ', function(){    varDragduration= New Date()- DragState.StartTime;    varOffsetleft= DragState.Currentleft - DragState.Startleft;    varOffsetTop= DragState.Currenttop - DragState.Starttop;    if(dragduration<  -){        varFiretap= Math.ABS(offsetleft)< Ten && Math.ABS(OffsetTop< Ten);        if(IsNaN(offsetleft)|| IsNaN(OFFSETTOP)){Firetap= true;        }        if(FIRETAP){            Alert(' Tap ');        }    }DragState= {};});

If the difference between the horizontal displacement and the vertical displacement is less than 10 pixels, touchstart and touchend The time difference is less than 300ms, the event is considered to be triggered Tap .

Scenario Two: Add Transitions animation

Since the transition animation on some models to compare the reasons for the card, then if you are not too considering the performance, you can add transition animation, about react the transition animation, the time is about 300ms good, Can look at my previous study of transition code: React-css3-transition-group

Scenario Three: Adding a mask layer to the target page

In the target page with a layer of transparent layers, so that the previous page click on this layer of failure, the practice is to use a high-level component, in the high-level components to add a timer, each page loaded with a layer of shells, 400ms after the disappearance of the bomb layer.

ImportReact from ' react ';ConstStyles= {    modal: {        width: ' 100% ',        Height: ' 100% ',        position: ' Absolute ',        Top: 0,         Left: 0,         Right: 0,        Bottom: 0,        ZIndex:  -,        BackgroundColor: ' Transparent '    }};ConstComponentwrapper=MyComponent= {    ConstComponenttemplate= React.Createclass({        getinitialstate(){            return {                modal: true            };        },        Componentdidmount(){             This.Modalinter = SetTimeout(()= {                 This.setState({ modal: false });            },  -);        },        Componentwillunmount(){             This.hidetips();             This.Modalinter && cleartimeout( This.Modalinter);        },        Render(){            return(<Page style={  This.Props.style }>                    <MyComponent{... This.Props }Container={  This } />{this.state.modal && <div style={styles.modal}></div> }                </page>            )        }    });return componenttemplate;};export default componentwrapper;

In addition 0.13.3 , when the version is react also supported mixins , you can add the following code:

ImportReact from ' react ';ImportReactdom from ' React-dom ';ConstStyles= {    modal: {        width: ' 100% ',        Height: ' 100% ',        position: ' Absolute ',        Top: 0,         Left: 0,         Right: 0,        Bottom: 0,        ZIndex:  -,        BackgroundColor: ' Transparent '    }};ConstClickthroughmixin= {    getinitialstate(){        return {            Clickthroughmodal: true        }    },    Componentdidmount(){         This._rendermodal();         This.Modalinter = SetTimeout(()= {             This._modaltarget && Reactdom.Unmountcomponentatnode( This._modaltarget);             This._modaltarget.Remove();        },  -);    },    Componentwillunmount(){         This.Modalinter && cleartimeout( This.Modalinter);    },    _rendermodal(){        if(! This._modaltarget){             This._modaltarget = Document.createelement(' div ');             This._container =  This._getcontainerdomnode().appendchild( This._modaltarget);            Reactdom.Unstable_rendersubtreeintocontainer( This,(<div style={ Styles.modal }></div>), This._modaltarget            );        }    },_getcontainerdomnode() {Const NODE = Reactdom.finddomnode( This), BODY = document.body;return node?Node.parentnode||Body:body;    }};export default clickthroughmixin;

Mobile App list point-through event processing method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.