JS Achieve star rating effect (shortest code)

Source: Internet
Author: User

1. Preface

This scheme is inspired by the JS single-line write a rating component, and wrote a simple demo.

The function has normal sliding, dynamic display the number of solid stars, when the click to confirm, the current number of solid stars, and then when the move is clicked, leave the previous state.

This demo does not have text messages and other prompts, can be added to use as needed.

2. Code

<! DOCTYPE Html>Star height is 21pxif (i <= 5 & i > 0&& pos.y < Offsettopval + + && pos.y > Offsettopval {obj.innerhtml= "★★★★★☆☆☆☆☆". Slice (5-i,10-i);//console.log ("Mousepage (x, y) = (" +pos.x+ "," +pos.y+ ") starnumber=" + Math.floor ((pos.x+6)/13.2));
$ (obj). Click (function () {laststarnum = i;})}} $ (' * '). MouseMove (function () {mOver ();}); function GetPos (EV) {var scrolltop=document.documentelement.scrolltop| | Document.body.scrollTop; var scrollleft=document.documentelement.scrollleft| | Document.body.scrollLeft; return {x:ev.clientx+scrollleft, y:ev.clienty+scrolltop}; }</script>

3. Summary

The highlight is the use of the "★★★★★☆☆☆☆☆" Slice (5-laststarnum,10-laststarnum), which does not require an online star rating system that is 5 li elements.

  

JS Achieve star rating effect (shortest code)

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.