本文实例为大家分享了JavaScript实现滑动选择日期的具体代码,供大家参考,具体内容如下

$(page).on('touchmove','#touchMoveTime',function (event) {
  touchMove(event);
 });
 scrollBarInit(); //初始化
 function scrollBarInit() {
  var defaultValue = 3,maxValue = 30;
  var myDate = new Date();
  var year = myDate.getFullYear();
  var month = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
  var date = myDate.getDate();
  var day = new Date(year,month,0);
  var daycount = day.getDate(); //获取本月天数:
  if((date + defaultValue) > daycount){
  if(month == 12){
   month = 1;
   year = year + 1;
  }else{
   month = month + 1;
  }
  date = (date + defaultValue) - daycount;
  }else{
  date = date + defaultValue;
  }
  if(month < 10){
  month = "0"+month;
  }
  if(date < 10){
  date = "0"+date;
  }

  $("#endTime").attr('value',year+'-'+month+'-'+date);
  var currentX = $("#touchMoveTime").width() * (0 / maxValue);
  $('#scroll_Track').css({width:currentX+"px"});
  $('#scroll_Thumb').css({transform:'translate(' + currentX + 'px, 0)'});

 };
 function touchMove(event) {
  event.preventDefault();
  if (!$('#scroll_Thumb') || !event.touches.length) return;
  var defaultValue = 3,maxValue = 30;
  var myDate = new Date();
  var year = myDate.getFullYear();
  var month = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
  var date = myDate.getDate();

  var tran_currentX = '';
  var startOffset = parseInt($('#touchMoveTime').offset().left);
  var endOffset = parseInt($('#touchRight').offset().left);
  var _limit = endOffset - startOffset;
  var touchMoveTimeOffsetLeft = $('#scroll_Track').offset().left;
  var touch = event.touches[0];
  var endX = touch.pageX;
  var currentX = endX - touchMoveTimeOffsetLeft;
  var Timevalue = Math.round(maxValue * (currentX / $("#touchMoveTime").width())); //当前刻度值
  if(Timevalue < defaultValue){
  Timevalue = defaultValue
  }else if(Timevalue > maxValue){
  Timevalue = maxValue;
  }
  if(currentX < _limit && currentX > 15){
  $('#days').text(Timevalue);
  $('#scroll_Track').css({width:currentX+"px"});
  if(currentX < 20){
   tran_currentX = 0
  }else{
   tran_currentX = currentX - 20;
  }
  $('#scroll_Thumb').css({transform:'translate(' + tran_currentX + 'px, 0)'});

  var day = new Date(year,month,0);
  var daycount = day.getDate(); //获取本月天数
  if((date + Timevalue) > daycount){
   if(month == 12){
   month = 1;
   year = year + 1;
   }else{
   month = month + 1;
   }
   date = (date + Timevalue) - daycount;
  }else{
   date = date + Timevalue;
  }
  if(month < 10){
   month = "0"+month;
  }
  if(date < 10){
   date = "0"+date;
  }
  $('#endTime').attr('value',year+'-'+month+'-'+date);
  }
 }
<div class="clList">
  <span class="cl-15 pull-left">3天</span>
 <div id="touchMoveTime" class="jzrqDiv cl-70 pull-left">
 <div id="scroll_Track"></div>
     <div class="spirit icon" id="scroll_Thumb">&#xe669;</div>
 </div>
 <span class="cl-15 pull-left text-right" id="touchRight">30天</span>
</div>
.jzrqDiv{position:relative; top:15px; height: 10px; border-radius: 20px; background: #efefef; box-shadow:inset 0 1px 2px rgba(0,0,0,.15); }
2 #scroll_Track{ position: absolute; top:0; height: 10px; border-radius: 20px; background: #2399dc; z-index: 10;}
3 .spirit {position: absolute; top:-9px; width: 30px;height: 30px;line-height: 30px;font-size: 30px;border-radius: 50%;color: #ddd; background: #fff; z-index: 11;transform: translate(0,0);}

JavaScript实现移动端滑动选择日期功能

更多关于滑动效果的专题,请点击下方链接查看:

javascript滑动操作汇总

jquery滑动操作汇总

以上就是本文的全部内容,希望对大家的学习有所帮助。

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。