---
title: "picker鼠标上下拖动选择内容"
author: "hiuman"
date: "2017-01-17T19:45:07Z"
tags: ["jQuery"]
categories: ["Web前端"]
slug: "picker"
draft: false
lastmod: "2017-01-18T13:57:10Z"
id: 2353563
url: "https://hiuman.iteye.com/blog/2353563"
---

\[b]\[size=small]\[color=blue]上次写这个的时候的博客：http://hiuman.iteye.com/blog/2324929 上次是网上搜的，这次是自己写的。 无论多少个input都可以点击～但是只有一种内容（弹出的内容），可以修改，如果想要有多种内容需要改关联的弹出内容。 欢迎指点～ 我导入的jquery是jQuery v1.9.1版本的。还加入了一个关闭的图片。\[/color]\[/size]\[/b] \[b]\[size=small]\[color=blue]效果图： （图片有点大。弹出的位置是正好正中间的。）\[/color]\[/size]\[/b] \[img]http://dl2.iteye.com/upload/attachment/0122/6765/e43d9170-75c9-3c87-a8a5-cc38fc5f1d39.png\[/img] \[img]http://dl2.iteye.com/upload/attachment/0122/6767/594100f4-5aae-3355-9655-d93b25f4d692.png\[/img] \[size=small]\[color=blue]\[b]代码如下： css：\[/b]\[/color]\[/size] \[code="css"] input { width: 100%; height: 30px; } .fixed\_bg { position: fixed; top: 0; left: 0; display: none; width: 100%; height: 100%; background: rgba(0, 0, 0, .5); } .fixed\_bg .alert { position: fixed; top: 50%; left: 50%; overflow: hidden; width: 250px; height: 160px; margin-top: -75px; margin-left: -125px; cursor: default; border-radius: 10px; background: #fafafa; } .fixed\_bg .alert\_title { line-height: 40px; position: relative; width: 100%; height: 40px; text-align: center; color: #fff; background: #387bf0; } .fixed\_bg .alert\_title { cursor: default; } .fixed\_bg .alert\_sure, .fixed\_bg .alert\_close { cursor: pointer; } .fixed\_bg .alert\_sure { position: absolute; right: 10px; } .fixed\_bg .alert\_close { position: absolute; left: 10px; width: 20px; height: 40px; background: url('close.png') no-repeat center; background-size: 20px; } .fixed\_bg .alert\_con { position: relative; width: 100%; height: 120px; } .fixed\_bg .alert\_list\_wrap { position: absolute; top: 8px; left: 50%; width: 40px; height: 100px; margin-left: -20px; } .fixed\_bg .alert\_list\_msg { position: relative; overflow: hidden; width: 100%; height: 100%; } .fixed\_bg .alert\_list\_curr\_top, .fixed\_bg .alert\_list\_curr\_bottom { position: absolute; top: 33px; width: 40px; height: 2px; background: red; } .fixed\_bg .alert\_list\_curr\_bottom { top: 67px; } .fixed\_bg .alert\_list\_msg\_con { position: absolute; top: -68px; width: 100%; } .fixed\_bg .alert\_list\_msg\_con a { font-weight: bold; line-height: 34px; display: block; width: 100%; height: 34px; text-align: center; transition: top 2s; -webkit-transition: top 2s; /* Safari \*/ } .fixed\_bg .alert\_list\_msg\_descr { position: absolute; top: 0; left: -1px; width: 100%; height: 100%; background: -webkit-gradient(linear, 0% 100%, 0% 0%, from(rgb(245, 245, 245)), color-stop(.48, rgba(245, 245, 245, 0)), color-stop(.52, rgba(245, 245, 245, 0)), to(rgb(245, 245, 245))); } input { margin-bottom: 10px; } \[/code] \[b]\[size=small]\[color=blue]js：\[/color]\[/size]\[/b] \[code="js"] var content = '' function setInputVal() { var conTop = parseInt($('.fixed\_bg .alert\_list\_msg\_con').css('top')); // 34 0 -34 var temp = conTop - 68; // temp --&gt; -34 -68 ... var conIndex = -(temp / 34); // conIndex --&gt; 1 2 $('#curr').text(Math.round(conIndex)); $('#curr').attr('data-msg', (68 - conIndex * 34)); // conTop content = $($('.fixed\_bg .alert\_list\_msg\_con a')\[Math.round(conIndex - 1)]).text(); } setInputVal() var oInputGroup = $('input.choose'); oInputGroup.each(function() { oInputGroup.unbind('click').bind('click', function() { var \_this = this; $('.fixed\_bg').css('display', 'block'); eventListen() $('.alert\_close').unbind('click').bind('click', function() { $('.fixed\_bg').css('display', 'none') setInputVal() $(\_this).val(content) }) $('.alert\_sure').unbind('click').bind('click', function() { $('.fixed\_bg').css('display', 'none') setInputVal() $(\_this).val(content) }) }) }) function eventListen() { var aLength = $('.fixed\_bg .alert\_list\_msg\_con a').length; var sign = ''; var sMoveStart = ''; //鼠标按下 $(window).on("mousedown", function(event) { if (sign == "mouseup" || sign == '') { sign = "mousedown"; sMoveStart = event.screenY; $(window).mousemove(function(event) { // top =(68-index\*34) if (sign == "mousedown") { var currIndex = $('#curr').text(); var currTop = $('#curr').attr('data-msg'); sMoveLenth = event.screenY - sMoveStart; // console.log(sMoveLenth); var moveheight = Math.round(sMoveLenth / 34); var temp = currTop - sMoveLenth; var resultIndex = currIndex - moveheight; if (resultIndex &lt;= 0) { currIndex = 1 currTop = 68 - (currIndex) * 34; } else if (resultIndex &gt;= aLength) { currIndex = aLength currTop = 68 - currIndex * 34; } else { currIndex -= moveheight currTop = parseInt(currTop) + parseInt(sMoveLenth); } $('.alert\_list\_msg\_con').css('top', currTop + 'px') } }) } }) $(window).mouseup(function() { if (sign == "mousedown") { sign = "mouseup" setInputVal() var conIndex = $('#curr').text() $('.alert\_list\_msg\_con').css('top', (68 - conIndex * 34) + 'px') } }) } \[/code] \[b]\[size=small]\[color=blue]html：\[/color]\[/size]\[/b] \[code="html"]

请选择确定

[1]() [2]() [3]() [4]() [5]() [6]() [7]() [8]()

\[/code]