灵活星级评价插件

http://www.jq22.com/yanshi291



Default

You need just to have a div to build the Raty.

1 2 3 4 5
<div id="star"></div>

$('#star').raty();

Score

Used when we want starts with saved rating.

1 2 3 4 5
$('#star').raty({ score: 3 });

Score callback

If you need to start you score depending of a dynamic value, you can to use callback for it.
You can pass any value for it, not necessarily a data- value. You can use a field value for example.

1 2 3 4 5
<div id="star" data-score="1"></div>

$('#star').raty({

  score: function() {

    return $(this).attr('data-score');

  }

});

Number

Changes the number of stars.

1 2 3 4 5 6 7 8 9 10
$('#star').raty({ number: 10 });

Number Max

Change the maximum of start that can be created.

1 2 3 4 5
$('#numberMax-demo').raty({

  numberMax: 5,

  number   : 500

});

Score Name

Changes the name of the hidden score field.
It can be submited on a form or captured via JavaScript to be sended via ajax.

1 2 3 4 5
$('#star').raty({ scoreName: 'entity[score]' });

Number callback

You can receive the number of stars dynamic using callback to set it.

1 2 3
<div id="star" data-number="3"></div>

$('#star').raty({

  number: function() {

    return $(this).attr('data-number');

  }

});

Read Only

You can prevent users to vote. It can be applied with or without score and all stars will receives the hint corresponding of the selected star.
Stop the mouse over the stars to see:

1 2 3 4 5
$('#star').raty({ readOnly: true, score: 3 });

Read Only callback

You can decide if the rating will be readOnly dynamically returning true of false on the callback.

1 2 3 4 5
$('#star').raty({

  readOnly: function() {

    return 'true becomes readOnly' == 'true becomes readOnly';

  }

});

No Rated Message

If readOnly is enabled and there is no score, the hint "Not rated yet!" will be shown for all stars. But you can change it.
Stop the mouse over the star to see:

1 2 3 4 5
$('#star').raty({

  readOnly  : true,

  noRatedMsg: "I'am readOnly and I haven't rated yet!"

});

Half Show

You can represent a float score as a half star icon.
This options is just to show the half star. If you want enable the vote with half star on mouseover, please check the option half.
The round options showed belows is just for the icon, the score keeps as float always.

Enabled

The round rules are:

  • Down: score <= x.25 the star will be rounded down;
  • Half: score >= x.26 and <= x.75 the star will be a half star;
  • Up: score >= x.76 the star will be rounded up.
1 2 3 4 5
$('#star').raty({ score: 3.26 });

Disabled

The rules becomes:

  • Down: score < x.6 the star will be rounded down;
  • Up: score >= x.6 the star will be rounded up;
1 2 3 4 5
$('#halfShow-demo').raty({

  halfShow: false,

  score   : 3.26

});

Round

You can customize the round values of the halfShow option.
We changed the default interval [x.25 .. x.76], now x.26 will round down instead of to be a half star.
Remember that the full attribute is used only when halfShow is disabled.
You can specify just the attribute you want to change and keeps the others as default.

1 2 3 4 5
$('#star').raty({

  round: { down: .26, full: .6, up: .76 },

  score: 3.26

});

Half

Enables the half star mouseover to be possible vote with half values.
If you want to vote with more precison than half value, please check the option precision.

1 2 3 4 5
$('#star').raty({ half: true });

Star Half

Changes the name of the half star.

1 2 3 4 5
$('#star').raty({

  half    : true,

  starHalf: 'half.png'

});

Click

Callback to handle the score and the click event on click action.
You can mension the Raty element (DOM) itself using this.

1 2 3 4 5
$('#star').raty({

  click: function(score, evt) {

    alert('ID: ' + $(this).attr('id') + "\nscore: " + score + "\nevent: " + evt);

  }

});

Hints

Changes the hint for each star by it position on array.
If you pass null, the score value of this star will be the hint.
If you pass undefined, this position will be ignored and receive the default hint.

1 2 3 4 5
$('#star').raty({ hints: ['a', null, '', undefined, '*_*']});

Path

Changes the path where your icons are located.
Set it only if you want the same path for all icons.
Don't mind about the last slash of the path, if you don't put it, we will set it for you.

$('#star').raty({ path: 'assets/images' });

Now we have the following full paths: assets/images/star-on.pngassets/images/star-off.png and so.

Star Off and Star On

Changes the name of the star on and star off.

1 2 3 4 5
$('#star').raty({

  starOff: 'img/off.png',

  starOn : 'http://icons.com/on.png'

});

Cancel

Add a cancel button on the left side of the stars to cacel the score.
Inside the click callback the argument code receives the value null when we click on cancel button.

x 1 2 3 4 5
$('#star').raty({ cancel: true });

Cancel Hint

Like the stars, the cancel button have a hint too, and you can change it.
Stop the mouse over the cancel button to see:

x 1 2 3 4 5
$('#star').raty({

  cancel    : true,

  cancelHint: 'My cancel hint!'

});

Cancel Place

Changes the cancel button to the right side.

1 2 3 4 5 x
$('#star').raty({

  cancel     : true,

  cancelPlace: 'right'

});

Cancel off and Cancel On

Changes the off and on icon of the cancel button.

x 1 2 3 4 5
$('#star').raty({

  cancel   : true,

  cancelOff: 'off.png',

  cancelOn : 'on.png'

});

Icon Range

It's an array of objects where each one represents a custom icon.
The range attribute is until wich position the icon will be displayed.
The on attribute is the active icon.
The off attribute is the inactive icon.

1 2 3 4 5
$('#star').raty({

  iconRange: [

    { range: 1, on: '1.png', off: '0.png' },

    { range: 2, on: '2.png', off: '0.png' },

    { range: 3, on: '3.png', off: '0.png' },

    { range: 4, on: '4.png', off: '0.png' },

    { range: 5, on: '5.png', off: '0.png' }

  ]

});

You can use an interval of the same icon jumping some number.
The range attribute must be in an ascending order.
If the value on or off is omitted then the attribute starOn and starOff will be used.

$('#iconRange-demo').raty({

  starOff  : '0.png',

  iconRange: [

    { range: 1, on: '1.png' },

    { range: 3, on: '3.png' },

    { range: 5, on: '5.png' }

  ]

});

Now we have all off icons as 0.png, icons 1 and 2 as 1.png, icon 3 as 3.png and icons 4 and 5 as 5.png.

Size

The size in pixel of the icon you will to use.
It changes the size for all icons.

x 1 2 3 4 5
$('#star').raty({

  cancel   : true,

  cancelOff: 'cancel-off-big.png',

  cancelOn : 'cancel-on-big.png',

  half     : true,

  size     : 24,

  starHalf : 'star-half-big.png',

  starOff  : 'star-off-big.png',

  starOn   : 'star-on-big.png'

});

Width

By default Raty calculates the width calculating the size of the stars plus the spaces.
But for some reason the calculated width not fits on your layout, you can change it manually.
If you want to avoid that Raty applies the style width on you wrapper, set it to false.

1 2 3 4 5
$('#star').raty({ width: 150 });

Target

Some place to display the hints or the cancelHint.

$('#star').raty({

  cancel: true,

  target: '#hint'

});

Your target can be a div.

x  1  2  3  4  5
 
<div id="hint"></div>

Your target can be a text field.

x  1  2  3  4  5
 
<input id="hint" type="text" />

Your target can be a textarea.

x  1  2  3  4  5
 
<textarea id="hint"></textarea>

Your target can be a select.

x  1  2  3  4  5
 
  •                 
  •                 
  •                 
  •                 
  •                 
  •                 
  •               
  • <select id="hint">
    
      <option value="">--</option>
    
      <option value="bad">bad</option>
    
      <option value="poor">poor</option>
    
      <option value="regular">regular</option>
    
      <option value="good">good</option>
    
      <option value="gorgeous">gorgeous</option>
    
    </select>
    
    

    Target Type

    You have the option hint or score to chosse.
    You can choose to see the score instead the hints using the value score.
    For the cancel button the value is empty.

    x  1  2  3  4  5
     
    <div id="hint"></div>
    
    
    $('#targetType-demo').raty({
    
      cancel    : true,
    
      target    : '#hint',
    
      targetType: 'number'
    
    });
    
    

    Target Keep

    If you want to keep the score into the hint box after you do the rating, turn on this option.

    x  1  2  3  4  5
     
    <div id="hint"></div>
    
    
    $('#star').raty({
    
      cancel    : true,
    
      target    : '#hint',
    
      targetKeep: true
    
    });
    
    

    Target Text

    Normally all target is keeped blank if you don't use the targetKeep option.
    If you want a default content you can use this option.

    1  2  3  4  5
     
    --
    $('#star').raty({
    
      target    : '#hint',
    
      targetText: '--'
    
    });
    
    

    Target Format

    You can choose a template to be merged with your hints and displayed on target.

    1  2  3  4  5
     
    $('#star').raty({
    
      target      : '#hint',
    
      targetFormat: 'Rating: {score}'
    
    });
    
    

    Mouseover

    You can handle the action on mouseover.
    The arguments is the same of the click callback.
    The options targettargetFormattargetKeeptargetText and targetType are abstractions of this callback. You can do it by yourself.

    1 2 3 4 5
    $('#star').raty({
    
      mouseover: function(score, evt) {
    
        alert('ID: ' + $(this).attr('id') + "\nscore: " + score + "\nevent: " + evt);
    
      }
    
    });
    
    

    Mouseout

    You can handle the action on mouseout.
    The arguments is the same of the mouseover callback.

    1 2 3 4 5
    $('#star').raty({
    
      mouseout: function(score, evt) {
    
        alert('ID: ' + $(this).attr('id') + "\nscore: " + score + "\nevent: " + evt);
    
      }
    
    });
    
    

    Precision

    You can get the right position of the cursor to get a precision score.
    The star is represented just as half and full star, but the score is saved with precision.
    When you enable this option the half options is automatically enabled and targetType is changed to score.

    x  1  2  3  4  5
     
    $('#star').raty({ precision: true });
    
    

    Space

    You can take off the space between the star.

    12345
    $('#star').raty({ space: false });
    
    

    Single

    You can turn on just the mouseovered star instead all from the first until the mouseovered one.

    1 2 3 4 5
    $('#star').raty({ single: true });
    
    

    Changing the settings globally

    You can change any option mentioning the scope $.fn.raty.defaults. + option_name. It must be called before you bind the plugin.

    $.fn.raty.defaults.path = assets;
    
    $.fn.raty.defaults.cancel = true;
    
    

    Options

    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.

    Functions

    $('#star').raty('score');
    
    

    Get the current score. If there is no score then undefined will be returned.

    $('#star').raty('score', number);
    
    

    Set a score.

    $('#star').raty('click', number);
    
    

    Click on some star. It always call the click callback if it exists.

    $('.star').raty('readOnly', boolean);
    
    

    Change the read-only state.

    $('#star').raty('cancel', boolean);
    
    

    Cancel the rating. The boolean parameter tells if the click will be called or not. If you ommit it, false it will be.

    $('#star').raty('reload');
    
    

    Reload the rating with the same configuration it was binded.

    $('#star').raty('set', { option: value });
    
    

    Reset the rating with new configurations. Only options especified will be overrided.

    $('#star').raty('destroy');
    
    

    Destroy the bind and gives you the raw element before the bind.


    x  1  2  3  4  5
     
    1.5
      run
      run
      run
        run
      run
      run
      run
      run
      run

    • 0
      点赞
    • 0
      收藏
      觉得还不错? 一键收藏
    • 0
      评论

    “相关推荐”对你有帮助么?

    • 非常没帮助
    • 没帮助
    • 一般
    • 有帮助
    • 非常有帮助
    提交
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值