博主信息
博文 22
粉丝 0
评论 0
访问量 16292
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
获取动态值案例,点击商品效果-2019年1月22日20:00
小淇的博客
原创
734人浏览过

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>获取滚动值的案例</title>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <style>
    *{
        margin: 0;
        padding: 0;
    }
    .content{
        width: 100%;
        height: 60px;
        background: rgba(160,3,162, 0.1);
        box-shadow: 1px 3px 7px #ccc;
        line-height: 60px;
        position: fixed;
    }
    .content_2{background: rgba(160,3,162,0.4)}
    form{
        width: 500px;
        height: 35px;
        position: relative;
        margin: 0 auto;
    }
    input{
        width: 480px;
        height: 35px;
        border-radius: 20px;
        border: none;
        /* 设置四个边框样式为none */
        outline:none;
        padding-left: 20px;
    }
    button{width: 70px;
            height:35px;
            /* 设置或检索弹性盒模型对象的子元素出现的順序为none */
            order:none; 
            border-top-right-radius: 20px;
            border-bottom-right-radius: 20px;
            border: none;
            color: #fff;
            position: absolute;
            right:0;
            top:14px;
            outline: none;
            font-weight: bold;
            background: rgba(160,3,162,0.4);}
            [placeholder]{color:rgba(160,3,162,0.6);}
    .pic{width: 70%;
        height: 580px;
        background: url(images/3.jpg);
        margin: 0 auto;
        }
    .box{width: 70%;
        height:1200px;
        background:rgba(108,108,106,0.1);
        margin: 0 auto;
        }
    </style>
</head>
<body>
<!-- 导航栏 -->
    <div class="content">
        <form>
            <input type="text" placeholder="#请输入关键词#">
            <button>全网搜索</button>
        </form>
    </div>
    <!-- 轮播图 -->
    <div class="pic"></div>
    <!-- 页面详情 -->
    <div class="box"></div>
    <script>
    $(function(){
        $(window).scroll(function(){
            // 获取浏览器滚动条到顶部的垂直高度(即网页被卷上去的高度)
            if($(window).scrollTop()>60){
                $('.content').css('display','none')
            }else{
                $('.content').css('display','block')
            }
            if($(window).scrollTop()>580){
                $('.content').addClass('.content_2').css('display','block')
            }else{
                $('.content').removeClass('content_2')
            }
    })
})
        // $('.content').css('display','none')
    </script>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

知识点:

scroll事件 当用户滚动指定的元素时触发

浏览器滚动条移动到距离顶部60PX的时候隐藏效果

实例

if($(window).scrollTop()>60){
                $('.content').css('display','none')
            }else{
                $('.content').css('display','block')
            }

浏览器滚动条移动到距离顶部580PX的时候显示效果

实例

if($(window).scrollTop()>580){
                $('.content').addClass('.content_2').css('display','block')
            }else{
                $('.content').removeClass('content_2')
            }


点击商品选中效果实例

<!DOCTYPE HTML>
<html>
<head>
<title>点击商品选中效果</title>
<meta charset="utf-8"/>
<link rel="icon" type="image/x-icon" href="images/2.png"> 
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<style type="text/css">
    * {margin: 0px auto;padding: 0px;}
    .top {width: 402px;height: 35px;
          line-height: 35px;text-align:center;
          margin-top: 50px;
          background: #C40000;color:#fff;
        }
    .main {width: 400px;height: 400px;border: 1px solid #C40000;}
    p {width: 400px;height: 26px;margin-top:10px;}
    b {width: 90px;height: 26px;
       line-height: 26px;text-align: center;
       font-size: 12px;color:#838383;
       border: 1px solid #ccc;
       float: left;margin-left: 5px;}
    span {width: 90px;height: 26px;line-height: 26px;
        text-align: center;font-size: 12px;
        color:#838383;border: 1px solid #ccc;
        display: block;float: left;margin-left: 5px;}
    span:hover {cursor: pointer;}
    button {width: 120px;height: 35px;background: #C40000;color: white;border: 0px;}
    button:hover {cursor: pointer;}
    .notice{
        border:0px;
    }
    .select{
        border:2px solid #C40000;
        width: 88px;
        height: 24px;
        line-height: 24px;
        color: red;
    }

</style>

</head>
<body>
<div class="top">请选择信息后加入购物车</div>
    <div class="main">
        <p class="item" name="version">
            <b class="notice">版本</b>
            <span>ONE A2001</span>
            <span>ONE A0001</span>
            <span>ONE A1001</span>
        </p>
        <p class="item" name="color">
            <b class="notice">机身颜色</b>
            <span>白色</span>
            <span>黑色</span>
            <span>金色</span>
        </p>
        <p class="item" name="type">
            <b class="notice">套餐类型</b>
            <span>标配</span>
            <span>套餐一</span>
            <span>套餐二</span>
        </p>
        <p class="item" name="ram">
            <b class="notice">运行内存</b>
            <span>2GB</span>
            <span>3GB</span>
            <span>4GB</span>
        </p>
        <p class="item" name="rom">
            <b class="notice">机身内存</b>
            <span>16GB</span>
            <span>32GB</span>
            <span>64GB</span>
        </p>
        <p class="item" name="location">
            <b class="notice">产地</b>
            <span>中国大陆</span>
            <span>港澳台</span>
        </p>
        <p class="item" name="price">
            <b class="notice">价格</b>
            <span>999元抢购</span>
        </p>
        <p class="item1" name="num">
<b class="notice">数量</b>
<!-- number是输入框 -->
<input type="number" value="1" style="width:40px;height:26px;">
        </p>

        <p style="margin-top:30px;margin-left:95px;">
            <button class="bu1" id='sub'>加入购物车</button>
        </p>        
</div>

<script>
  $(function(){
        $("span").click(function(){
       if($(this).hasClass("select")){// 匹配span中是否带有class为select
           $(this).removeClass("select");//含有就清除
       }else {
           $(this).addClass("select").siblings("span").removeClass("select");
       }//没有就添加上select的class名,同时匹配同级span,清除同级其他span中的选中样式
         });
$('#sub').click(function(){
let form={}//创建一个对象, 用来存选中的数据的
let flag=true //能不能加入购物车
// 判断是否每个选项都选中; 没选中则弹窗警告; 选中了则添加至需要发送的表单数据里面
$('.item').each(function(){//each() 方法规定为每个匹配元素规定运行的函数
   if($(this).children('span.select').length!=1){
     // alert($(this).find('.notice').html()+'未选中')
     flag=false;
   }else{
let key=$(this).attr('name');//获取当前p标签的 name属性值 不同的p属性名不一样
let value=$(this).children('span.select').html()//获得被选中的商品类型
form[key]=value//将键值对一一对应
   }

})
//判断数量最少为1
// val()设置或返回表单字段的值
if($('.item1 input').val()<=0){
    alert('数量最少为1')
    flag=false;
}else{
    //将用户输入商品件数放进form中
    form['num']=$('.item1 input').val()
    console.log(form)
}
if(flag) {
    alert('可以加入购物车了')
}
})
});
</script>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

总结:

一:检查span是否带有class为select

    含有就清除,否则就增加

实例

if($(this).hasClass('select')){
$(this).removeClass('select')
}else{
$(this).addClass('select')
}

二:siblings()方法

实例

.siblings('span').removeClass('select')



批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学