jQuery滑动星星评分效果

每日分享效果,今天分享一个jQuery滑动星星评分效果。

jQuery星星评分制作5颗星星鼠标滑过评分打分效果,可取消评分结果,重新打分。

HTML代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="css/css.css">
    <script src="js/jquery.js"></script>
</head>
<body>
<div id="starRating">
    <p class="photo">
        <span><i class="high"></i><i class="nohigh"></i></span>
        <span><i class="high"></i><i class="nohigh"></i></span>
        <span><i class="high"></i><i class="nohigh"></i></span>
        <span><i class="high"></i><i class="nohigh"></i></span>
        <span><i class="high"></i><i class="nohigh"></i></span>
    </p>
    <p class="starNum">0.0分</p>
    <div class="bottoms">
        <a class="garyBtn cancleStar">取消评分</a><a class="blueBtn sureStar">确认</a>
    </div>
</div>
<script>
    $(function () {
        //评分
        var starRating = 0;
        $(‘.photo span‘).on(‘mouseenter‘,function () {
            var index = $(this).index()+1;
            $(this).prevAll().find(‘.high‘).css(‘z-index‘,1)
            $(this).find(‘.high‘).css(‘z-index‘,1)
            $(this).nextAll().find(‘.high‘).css(‘z-index‘,0)
            $(‘.starNum‘).html((index*2).toFixed(1)+‘分‘)
        })
        $(‘.photo‘).on(‘mouseleave‘,function () {
            $(this).find(‘.high‘).css(‘z-index‘,0)
            var count = starRating / 2
            if(count == 5) {
                $(‘.photo span‘).find(‘.high‘).css(‘z-index‘,1);
            } else {
                $(‘.photo span‘).eq(count).prevAll().find(‘.high‘).css(‘z-index‘,1);
            }
            $(‘.starNum‘).html(starRating.toFixed(1)+‘分‘)
        })
        $(‘.photo span‘).on(‘click‘,function () {
            var index = $(this).index()+1;
            $(this).prevAll().find(‘.high‘).css(‘z-index‘,1)
            $(this).find(‘.high‘).css(‘z-index‘,1)
            starRating = index*2;
            $(‘.starNum‘).html(starRating.toFixed(1)+‘分‘);
            alert(‘评分:‘+(starRating.toFixed(1)+‘分‘))
        })
        //取消评分
        $(‘.cancleStar‘).on(‘click‘,function () {
            starRating = 0;
            $(‘.photo span‘).find(‘.high‘).css(‘z-index‘,0);
            $(‘.starNum‘).html(starRating.toFixed(1)+‘分‘);
        })
        //确定评分
        $(‘.sureStar‘).on(‘click‘,function () {
            if(starRating===0) {
                alert(‘最低一颗星!‘);
            } else {
               alert(‘评分:‘+(starRating.toFixed(1)+‘分‘))
            }
        })
    })
</script>
</body>
</html>

CSS代码:

#starRating .photo span {
    position: relative;
    display: inline-block;
    width: 44px;
    height: 42px;
    overflow: hidden;
    margin-right: 23px;
    cursor: pointer;
}
#starRating .photo span:last-child {
    margin-right: 0px;
}
#starRating .photo span .nohigh {
    position: absolute;
    width: 44px;
    height: 42px;
    top: 0;
    left: 0;
    background: url("../img/star.png");
}
#starRating .photo span .high {
    position: absolute;
    width: 44px;
    height: 42px;
    top: 0;
    left: 0;
    background: url("../img/star1.png");
}
#starRating .starNum {
    font-size: 26px;
    color: #de4414;
    margin-top: 4px;
    margin-bottom: 10px;
}
#starRating .bottoms {
    height: 54px;
    border-top: 1px solid #d8d8d8;
}
#starRating .photo {
    margin-top: 30px;
}
#starRating .bottoms a {
    margin-bottom: 0;
}
#starRating .bottoms .garyBtn {
    margin-right: 57px!important;
}
#starRating .bottoms a {
    width: 130px;
    height: 35px;
    line-height: 35px;
    border-radius: 3px;
    display: inline-block;
    font-size: 16px;
    transition: all 0.2s linear;
    margin: 16px 0 22px;
    text-align: center;
    cursor: pointer;
}
.garyBtn {
    margin-right: 60px!important;
    background-color: #e1e1e1;
    color: #999999;
}
.blueBtn {
    background-color: #1968b1;
    color: #fff;
}
.blueBtn:hover {
    background: #0e73d0;
}

原文地址:https://blog.51cto.com/14535203/2447819

时间: 2024-08-03 17:00:46

jQuery滑动星星评分效果的相关文章

jquery实现星星评分

不得不说,jquery实在太强大了.星星打分,本打算用switch case来遍历了,好在只有五颗星.想想不甘心,去查了下jquery的API,果然找到两个超级实用的选择器:prevAll和nextAll.点击某颗星时,之前的星都填满,之后的星为空.这两个选择器就像是专为此种情况而生的.星星打分的功能就这样轻松实现了. 上图: 上代码: HTML: <table class="tab_star"> <tr> <td>1.口齿是否清楚口齿是否口齿是否清

iOS 8:CALayer实现星星评分效果

要实现如下图所示效果,一种思路是封装一个UIView,提供星星个数属性,在属性中赋值并调用[self setNeedsLayout]令视图重绘(之后系统会调用layoutSubviews),覆盖layoutSubviews,在此方法内进行单个星星的frame调整.由于每个星星不需要事件处理,在UIView派生类中添加多个(本例为5个)CALayer,设置其contents属性为CGImage即可.

基于 jQuery 实现垂直滑动的手风琴效果

今天我们要与大家分享一个漂亮而灵活的垂直 jQuery 手风琴效果.其主要思想是扩大手风琴片上的点击和显示更多的信息.其他内容片段将变得不那么透明.当使用一个导航箭头导航下一个片段,新的片会从顶部或底部滑动. 效果演示     插件下载 HTML 示例代码: <div id="va-accordion" class="va-container"> <div class="va-nav"> <span class=&q

jQuery 滑动菜单效果

<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <style> *{ padding:0; margin:0; } ul,li{ list-style:none; } .nav{ height:30px; background:rg

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();//使用属性da

[Jquery]滑动门效果

$(function(){    var $box=$("#box");    var $img=$box.find("img");    var imgWidth=$img.eq(0).width();    var exposeWidth=160;    var boxWidth=imgWidth+exposeWidth*($img.length-1);    var translate=imgWidth-exposeWidth;    $box.width(b

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();//使用属性da

星星评分

一.方法1 1.用到图片 2.结构和样式 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> ul { padding-left: 0; overflow: hidden; } ul li { float: left; list-style: no

【回忆1314】回忆之星星评分

直接看效果点这里 HTML <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title> 星星评分 </title> <link rel="stylesheet" href="star.css"/> </head> <body> <ul