您好,登錄后才能下訂單哦!
本文實例講述了jQuery Raty星級評分插件使用方法。分享給大家供大家參考,具體如下:
使用jQuery Raty,可以很方便的在頁面上嵌入一個評分組件,如下所示:
使用方法很簡單,首先從https://github.com/wbotelhos/raty下載raty的源代碼(依賴于jquery)
然后在頁面中引入相應的js文件、css文件、圖片資源,在需要添加評分組件的元素上(比如span標簽)添加下面的jquery代碼即可:
$('span').raty();
以上為jQuery Raty的缺省使用方法,此外,該組件還支持豐富的傳入參數和回調函數,例如:
設置jQuery Raty的初始評分:
評分回調函數
如果需要根據后臺動態設置初始評分,可以使用回調函數實現。例如使用div中的data-attribute屬性:
<div data-score="1"></div>
$('div').raty({ score: function() { return $(this).attr('data-score'); } });
還可以改變星星的個數:
$('div').raty({ number: 10 });
只讀模式:
$('div').raty({ readOnly: true, score: 3 });
點擊事件:
$('div').raty({ click: function(score, evt) { alert('ID: ' + this.id + "\nscore: " + score + "\nevent: " + evt); } });
路徑:
變更圖標保存的位置,所有圖標需要位于同一目錄下,路徑結尾的/不添加也可以
<div data-path="assets/images"></div>
$('div').raty({ path: function() { return this.getAttribute('data-path'); } });
取消評分:
$('div').raty({ cancel: true });
全局改變設置:
你可以全局更改上述提到的所有設置 $.fn.raty.defaults.OPTION = VALUE;
. 該語句必須添加在插件綁定之前。
$.fn.raty.defaults.path = assets; $.fn.raty.defaults.cancel = true;
參數列表:
cancel : false // Creates a cancel button to cancel the rating. cancelClass : 'raty-cancel' // Name of cancel's class. 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 : undefined // 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. 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. targetScore : undefined // Element selector where the score will be filled, instead of creating a new hidden field (scoreName option). targetText : '' // Default text setted on target. targetType : 'hint' // Option to choose if target will receive hint o 'score' type. starType : 'img' // Element used to represent a star.
回調函數列表:
$('div').raty('score'); // Get the current score. $('div').raty('score', number); // Set the score. $('div').raty('click', number); // Click on some star. $('div').raty('readOnly', boolean); // Change the read-only state. $('div').raty('cancel', boolean); // Cancel the rating. The last param force the click callback. $('div').raty('reload'); // Reload the rating with the current configuration. $('div').raty('set', { option: value }); // Reset the rating with new configurations. $('div').raty('destroy'); // Destroy the bind and give you the raw element. $('div').raty('move', number); // Move the mouse to the given score point position.
更多關于jQuery相關內容感興趣的讀者可查看本站專題:《jQuery擴展技巧總結》、《jQuery常用插件及用法總結》、《jQuery切換特效與技巧總結》、《jQuery遍歷算法與技巧總結》、《jQuery常見經典特效匯總》、《jQuery動畫與特效用法總結》及《jquery選擇器用法總結》
希望本文所述對大家jQuery程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。