JS exercise function Code and image

Source: Internet
Author: User

To implement the center of the element:

A is the width or height of the element, and L is the width or height of the page

e.style.left=l/2-a/2+ ' px ';

Implementing the movement of elements

The left margin of the mouse distance element on the element is derived from the MouseDown event.

C=f-h

The left margin of the element distance to the page is drawn, in the MouseMove event

e.style.left=f-c+ ' px '

Implement collision detection of elements

L1 and R1 are the same element, L2 and R2 are the same element, L1,r1 imagined as the left end of the page, L2,r2 imagined as the right end of the page

L1,R1 is the left side of the element page distance, L2,R2 is the right side of the element page distance

As long as the satisfaction of L1<L2<-->R2<R1 is satisfied, L2 and R2 any point in the range of L1 and R1, then the x-axis collision is established

Loop I=L2<-->R2

In the loop body if (L1<I<R1)

Boolean values X_flag and Y_flag record X-and y-axis collision detection data, with (and) (&&) operations

Knowledge Point Supplement:

1<x && x<6//The value of x is true between 1~6 and true.

1<x | | X<6//The value of x is X>1 infinity, x<6 is true when infinite number, one of which is true

JS exercise function Code and image

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.