jquery模拟实现仿select效果

本着服务为人民的远大理想,最近写了个jquery模拟select效果的小东东,挺好用,分享下。

可以直接在我上传的资源里下载压缩包,下载地址:jquery模拟实现仿select效果

首先,上html页面代码

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>js模拟select</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>

<body>
<div class="container">
  <div class="select-box select-city">
    <div class="select-header"> 省份 </div>
    <ul class="select-content">
      <li>北京</li>
      <li>河北省</li>
      <li>山东省</li>
    </ul>
  </div>
  <div class="select-box select-city">
    <div class="select-header"> 城市 </div>
    <ul class="select-content">
      <li>北京</li>
    </ul>
  </div>
  <div class="select-box select-city">
    <div class="select-header"> 区/县 </div>
    <ul class="select-content">
      <li>海淀区</li>
      <li>朝阳区</li>
      <li>东城区</li>
    </ul>
  </div>
</div>
</body>
<script src="js/jquery.min.js"></script>
<script src="js/select.js"></script>
</html>

然后,上css代码

@charset "utf-8";
body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,p,blockquote,th,td,p{margin:0;padding:0; font-family:"微软雅黑", Microsoft Yahei, Arial, Helvetica, sans-serif;}
ol,ul{list-style:none;}
.container{ padding-top:100px; margin-left:100px;}

/**
* 模拟select
**/
.select-box { position: relative; float: left; margin-right: 8px; }
.select-city .select-header { width: 88px; height: 36px; border: 1px solid #c1c1c1; padding-right: 26px; background: url(img/arrow.png) no-repeat #f9f9f9; background-position: 95px 15px; line-height: 36px; text-indent: 14px; font-size: 16px; color: #a9a9a9; cursor: pointer; overflow: hidden; }
.select-city .select-arrow { background-position: 95px -9px; }
.select-city .select-content { border-left: 1px solid #c1c1c1; border-right: 1px solid #c1c1c1; position: absolute; top: 38px; left: 0; z-index: 99; display: none; background-color: #f9f9f9; }
.select-city .select-content li { width: 114px; height: 36px; margin: 0; border-bottom: 1px solid #c1c1c1; line-height: 36px; text-indent: 14px; background-color: #f9f9f9; font-size: 16px; color: #a9a9a9; cursor: pointer; }
.select-bank .select-header { width: 272px; height: 36px; border: 1px solid #c1c1c1; padding-right: 26px; background: url(img/arrow.png) no-repeat #f9f9f9; background-position: 279px 15px; line-height: 36px; text-indent: 14px; font-size: 16px; color: #a9a9a9; cursor: pointer; overflow: hidden; }
.select-bank .select-arrow { background-position: 279px -9px; }
.select-bank .select-content { border-left: 1px solid #c1c1c1; border-right: 1px solid #c1c1c1; position: absolute; top: 38px; left: 0; z-index: 99; display: none; background-color: #f9f9f9; }
.select-bank .select-content li { width: 298px; height: 36px; margin: 0; border-bottom: 1px solid #c1c1c1; line-height: 36px; text-indent: 14px; background-color: #f9f9f9; font-size: 16px; color: #a9a9a9; cursor: pointer; }

最后,上js代码:

$(function(){
	/**
     * 模拟select
     **/
    $(".select-header").click(function(){
        $(this).parent().siblings(".select-box").find(".select-content").slideUp("fast");
        if($(this).siblings(".select-content").is(":hidden")){
            $(this).addClass("select-arrow");
            $(this).siblings(".select-content").slideDown("fast");
            var evt =  new Object;
            if ( typeof(window.event) == "undefined" ){//如果是火狐浏览器
                evt = arguments.callee.caller.arguments[0];
            }else{
                evt = event || window.event;
            }
            evt.cancelBubble = true;
        }else{
            $(this).removeClass("select-arrow");
            $(this).siblings(".select-content").slideUp("fast");
			//去除事件冒泡
            var evt =  new Object;
            if ( typeof(window.event) == "undefined" ){//如果是火狐浏览器
                evt = arguments.callee.caller.arguments[0];
            }else{
                evt = event || window.event;
            }
            evt.cancelBubble = true;
        }
    });
	$(document).click(function(){
        $(".select-header").removeClass("select-arrow");
        $(".select-content").slideUp("fast");
    });
    $(".select-content li").on("click",function(){
        $(this).parent().siblings(".select-header").removeClass("select-arrow");
        $(this).parent().siblings(".select-header").text($(this).text()).end().slideUp("fast");
    });
    $(".select-content li").hover(function(){
        $(this).css("background-color","#cfcfcf");
    },function(){
        $(this).css("background-color","#fff");
    });
});

效果如下:

有任何交流和指教,可加我QQ:1740437

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-02 20:45:10

jquery模拟实现仿select效果的相关文章

jQuery模拟无刷新分页效果

<html> <head> <title>jQuery模拟无刷新分页效果|河北苗木|河北金梆子锅炉</title> <script src="/images/jquery-1.4.2.min.js" type="text/javascript"></script> <style type="text/css"> body { font-size:12px; col

仿jquery mobile中的select控件效果

不说废话,直接上代码,最好将on.png和off.png也使用jquery mobile里的图片 //仿jQuery mobile Select控件 //用法box为容器id,_id指控件id,selectvalue为选中值,Value为当前值 function SelectBox(box,_id,selectvalue,Value) { if(Value != selectvalue) { $("#" + box).html("<input type=hidden v

【技术】jquery模拟select下拉框取值效果

<!DOCTYPE html><html lang="en"><head><title>jquery模拟select框</title><meta charset="utf-8"><style>body{padding:0;margin:0;font-size:12px;}ul,li{list-style:none;padding:0;margin:0;}#dropdown{width

使用jQuery模拟Google的自动提示效果

注意: 1.本功能使用SqlServler2000中的示例数据库Northwind,请打SP3或SP4补丁:2.请下载jQuery组件,河西FTP中有下载:3.本功能实现类似Google自动提示的效果,通过ajax引擎从服务器获取,返回XML数据:4.本示例程序没考虑性能问题:5.不支持Firefox浏览器,因为该浏览器没有propertychange事件. 步骤: 1.创建一个名为GoogleServlet的Servlet,负责从数据库中读取数据并生成XML格式的数据. package com

360浏览器搜索框下拉选择图片js模拟select效果

最近360浏览器网址导航的主页增加了一个下拉选择图片搜索的功能,也就是用js模拟出了select的效果,今天在单位闲了无事干,就把空上功能给摸索出来了,虽然做的不是太完善,但对要求不高的用户来说,已经可以了,而且也可以为学习Js的朋友提供参考. <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <style>

7款基于jquery的web前端的效果预览及源码下载

1.纯css3简单实用的checkbox复选框和radio单选框 简单实用的checkbox复选框和radio单选框.界面清淅.舒服. 在线演示 源码下载 2.jQuery实现重力弹动模拟效果特效 jQuery实现重力弹动模拟效果特效,鼠标经过两块黑色div中间的红色线时,下方的黑快会突然掉落,并在掉落地上那一刻出现了弹跳的jquery特效效果,非常不错. 在线演示 源码下载 3.基于jQuery打造的选项卡向上弹出jquery焦点图切换特效 基于jQuery打造的选项卡向上弹出jquery焦点

手机端touch事件 jquery模拟

ontouchstart实现手机触屏中的hover效果 ontouchstart实现手机触屏中的hover效果 最近一直都在做手机网站的项目,会经常遇到手机屏幕触摸无交互效果,今天在一篇文章中看到模拟网页端hover效果的例子拿出来和大家分享下,已经使用效果还不错,大家可以根据下面的案例学下: 一.css样式: <style type="text/css">.inner { width: 100%; height: 100px; position: relative; }.

jquery自定义插件实现分页效果

这节介绍如何自定义jquery插件,实现分页效果,话不多说,先看看实现的效果: 分页插件 实现的代码如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta charset="utf-8"> <title>page t

经典!HTML5 Canvas 模拟可撕裂布料效果

这是一个模拟可撕裂布料效果的 HTML5 Canvas 应用演示,效果逼真.你会看到,借助 Canvas 的强大绘图和动画功能,只需很少的代码就能实现让您屏息凝神的效果. 温馨提示:为保证最佳的效果,请在 IE10+.Chrome.Firefox 和 Safari 等现代浏览器中浏览. 源码下载     效果演示 您可能感兴趣的相关文章 Web 开发中很实用的10个效果[源码下载] 精心挑选的优秀jQuery Ajax分页插件和教程 12个让人惊叹的的创意的 404 错误页面设计 让网站动起来!