HTML5商城開發三 jquery 星星評分插件


展示:

 

實現方法:

1.html引用star-grade.js

<script type="text/javascript" src="Scripts/star-grade.js"></script>
    <script type="text/javascript"> 
        $(document).ready(function () {
            $(".sstar").BindStars();//使用屬性data-score獲取評分值 $("#pye").SetStars(3);//傳分數,自動列出星星 }); </script>
<body>
    <div class="starscore sstar">
        <i></i>
        <i></i>
        <i></i>
        <i></i>
        <i></i>
    </div>
    <span id="ye"></span>
    <div class="starscore" id="pye"></div>

    <div class="starscore starscore_sm" >
        <i class="inred"></i>
        <i class="inred"></i>
        <i></i>
        <i></i>
        <i></i>
    </div>
    <div class="starscore starscore_lg">
        <i class="onred"></i>
        <i class="onred"></i>
        <i></i>
        <i></i>
        <i></i>
    </div>
</body>

2.css樣式

@charset "utf-8";
/* CSS Document */
.starscore {
    width: 200px;
    height: 30px;
}

    .starscore i {
        width: 14px;
        height: 14px;
        background: url(images/gray.gif) no-repeat; /* 14x14的灰色星星圖標 */
        display: inline-block;
        vertical-align: middle;
        background-size: contain; 
    }

        .starscore i.inred, .starscore i.onred {
            background: url(images/yel.gif) no-repeat; /* 14x14的黃色星星圖標 */
        }

.starscore_lg > i {
    width: 18px;
    height: 18px;
}

.starscore_sm > i {
    width: 12px;
    height: 12px;
}

3.插件js源碼

/*
*   jq擴展--星星評分插件
*
*   通過對象的屬性data-score獲取評分值
*   星星使用元素i表示,綁定星星背景圖
*   鼠標進入、離開事件的綁定樣式為inred,改變背景圖
*   點擊事件的綁定樣式為onred,改變背景圖
*/
(function ($) { "use strict"; $.fn.BindStars = function () { var starElement = $(this); starElement.children("i").mouseleave(function () { starElement.find("i").each(function (index) { $(this).removeClass("inred"); }); }); starElement.children("i").mouseenter(function () { var curIndex = starElement.find("i").index(this); starElement.find("i").each(function (index) { if (index <= curIndex) { $(this).addClass("inred"); } else { $(this).removeClass("inred"); } }); }); starElement.children("i").click(function () { var curIndex = starElement.find("i").index(this); starElement.find("i").each(function (index) { if (index <= curIndex) { $(this).addClass("onred"); } else { $(this).removeClass("onred"); } }); starElement.attr("data-score", curIndex + 1); }); }; $.fn.SetStars = function (score) { var scoreStr = ""; for (var i = 0; i < 5; i++) { if (i < score) { scoreStr += "<i class='onred'></i>"; } else { scoreStr += "<i></i>"; } } $(this).html(scoreStr); }; })(window.jQuery);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM