• jquery实现仿商品星级评价


    一,HTML部分
    <div id="rating-star">
        <a href="#">0</a>
        <a href="#">1</a>
        <a href="#">2</a>
        <input type="hidden" id="goodLevel" />
    </div>
    二,CSS部分
     
    接着给rating-star和链接一个样式,切换图片的关键只需要修改background-position就可以了:
     
    #rating-star {
        margin: 50px;
    }
     
        #rating-star a {
            background: url(commentstar.png) no-repeat 0 -90px;
            display: inline-block;
            height: 23px;
            text-indent: -999em;
            23px;
        }

    三,Js代码部分(关键的脚本)

    $('#rating-star').on('click', 'a', function () {
        $('#goodLevel').val(this.innerHTML);
    }).on('mouseenter', 'a', function () {
        setStar(this);
    }).on('mouseleave', 'a', function () {
        var level = $('#goodLevel').val();
        var $stars = $('#rating-star > a');
        if (level == '') {
            $stars.css('background-position', '0 -90px');
        } else {
            setStar($stars[level]);
        }
    });
     
    function setStar(star) {
        var $this = $(star);
        var level = $this.html();
        var n;
        if (level == '2') {
            n = '0 -30px';
        } else if (level == '1') {
            n = '0 0';
        } else {
            n = '0 -60px';
        }
        $this.prevAll().andSelf().css('background-position', n);
        $this.nextAll().css('background-position', '0 -90px');
    }
    blog首页:http://www.cnblogs.com/hubgit/
  • 相关阅读:
    基于视网膜虹膜识别的内容分级系统
    C# 反射详解一
    C# 委托浅析
    .Net Core中使用Dapper构建泛型仓储
    C# 泛型详解
    非对称可逆加密RSA
    对称可逆加密Des
    .NET Core 3.0 中间件 Middleware
    .NET Core3.0 日志 logging
    .Net Core3.0依赖注入DI
  • 原文地址:https://www.cnblogs.com/hubgit/p/5774646.html
Copyright © 2020-2023  润新知