Micro-credit Program five-star score (including Half star scoring) instance code _javascript

Source: Internet
Author: User

Mini-letter Program five-star rating

A classmate said he wanted to write a five-star score. A score of half stars.

So I made a toy. There is time to do the modular, the code can not look down.

Code:

1.index.wxml

<!--index.wxml-->

<block wx:for= "{{stars}} ' > <image class=" star-image "style=" left

 : {{item* 150}}rpx "src=" {{key > item? Key-item = = 0.5?halfsrc:selectedsrc): normalsrc}} ">

  <view class=" item "style=" Left:0rpx "data-key=" {{item+ 0.5}} "bindtap=" Selectleft ></view>

  <view class= "item" style= "Left:75rpx" data-key= "{{item+1}}" bindtap= "Selectright" ></view>

 </image>

</block>

2.index.wxss

. star-image {

 position:absolute;

 TOP:50RPX;

 WIDTH:150RPX;

 HEIGHT:150RPX;

 SRC: ". /.. /images/normal.png ";

}



. item {

 Position:absolute;

 TOP:50RPX;

 WIDTH:75RPX;

 HEIGHT:150RPX;

}

3.index.js

Index.js

//csdn Micro-Letter Applet Development column: http://blog.csdn.net/column/details/13721.html

//Get application instance

var app = Getapp ()

Page {

 data: {stars

  : [0, 1, 2, 3, 4],

  normalsrc: '. /.. /images/normal.png ',

  selectedsrc: '. /.. /images/selected.png ',

  halfsrc: '. /.. /images/half.png ',

  key:0,//rating

 },

 onload:function () {

 },

 //Click on the right, half star

 Selectleft: Function (e) {

  var key = E.currenttarget.dataset.key

  if (This.data.key = = 0.5 && E.currenttarget.dataset.key = = 0.5) {

   ///Only one star, click again, change to 0

   key = 0;

  }

  Console.log ("get" + key + "min")

  this.setdata ({

   key:key

  })



 },

 //Click on the left, the whole star

 Selectright: Function (e) {

  var key = E.currenttarget.dataset.key

  Console.log ("get" + key + "min")

  this.setdata ({

   Key:key

  })

 }

)

Code download

Star.rar

Thank you for reading, I hope to help you, thank you for your support for this site!

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.