Thứ Tư, 6 tháng 3, 2013

Tạo đánh giá với Jquery Raty

Đây là link trang web :http://wbotelhos.com/raty
Các bạn down về và đặt thành cây thư mục như hình vẽ:

File code chính là test4.html
Cách dùng rất đơn giản :
Bạn vào trong folder lib,mở file jquery.raty.js lên, kéo xuống cuối file có đoạn code:

Đây là giải thích các option của nó:
cancel : false // Creates a cancel button to cancel the rating.
cancelHint : 'Cancel this rating!' // The cancel's button hint.
cancelOff : 'cancel-off.png' // Icon used on active cancel.
cancelOn : 'cancel-on.png' // Icon used inactive cancel.
cancelPlace : 'left' // Cancel's button position.
click : undefined // Callback executed on rating click.
half : false // Enables half star selection.
halfShow : true // Enables half star display.
hints : ['bad', 'poor', 'regular', 'good', 'gorgeous'] // Hints used on each star.
iconRange : undefined // Object list with position and icon on and off to do a mixed icons.
mouseout : undefined // Callback executed on mouseout.
mouseover : undefined // Callback executed on mouseover.
noRatedMsg : 'Not rated yet!' // Hint for no rated elements when it's readOnly.
number : 5 // Number of stars that will be presented.
numberMax : 20 // Max of star the option number can creates.
path : '' // A global locate where the icon will be looked.
precision : false // Enables the selection of a precision score.
readOnly : false // Turns the rating read-only.
round : { down: .25, full: .6, up: .76 } // Included values attributes to do the score round math.
score : undefined // Initial rating.
scoreName : 'score' // Name of the hidden field that holds the score value.
single : false // Enables just a single star selection.
size : 16 // The size of the icons that will be used.
space : true // Puts space between the icons.
starHalf : 'star-half.png' // The name of the half star image.
starOff : 'star-off.png' // Name of the star image off.
starOn : 'star-on.png' // Name of the star image on.
target : undefined // Element selector where the score will be displayed.
targetFormat: '{score}' // Template to interpolate the score in.
targetKeep : false // If the last rating value will be keeped after mouseout.
targetText : '' // Default text setted on target.
targetType : 'hint' // Option to choose if target will receive hint o 'score' type.
width : undefined // Manually adjust the width for the project.

Các bạn đọc chắc cũng hiểu được chức năng của nó , chúng ta thêm vào chỗ path: '', thành lib/img ,tức nó sẽ liên quan tới file mà chúng ta code.
Bắt đầu nào:
Bạn cần 1 thẻ div có id="star":


Code toàn bộ sẽ là :

Muốn bắt đầu đánh giá ở vị trí mấy ta dùng:

Nếu bạn muốn đánh giá với giá trị tự động,các bạn dùng :
<div id="star" data-score="1"></div>
$('#star').raty({
score: function() {
return $(this).attr('data-score');
}
});

Như vậy mỗi khi bạn muốn đặt giá trị ,thay đổi giá trị đó,hoặc lấy giá trị đó, bạn chỉ cần thay đổi data-score= mấy thôi.
Ngoài ra còn có :
$('#star').raty({ number: 10 });
Mục đích để đặt số lượng sao.
$('#star').raty({ readOnly: true, score: 3 });
Dùng để áp dụng cho mục mà bạn muốn ngăn không cho mọi người vote nữa .Các bạn tư tìm hiểu thêm nhé.

Không có nhận xét nào:

Đăng nhận xét