如何用图像创建JavaScript / HTML5 Spinner列表? [英] How to create JavaScript/HTML5 Spinner List with images?

查看:109
本文介绍了如何用图像创建JavaScript / HTML5 Spinner列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:使用图片(非文字)创建Javascript / HTML5微调器列表,以便用户输入重量和高度。 (示例图片/资产如下)。

Objective: Create a Javascript/HTML5 spinner list using images (not text) so user may enter weight and height. (example images/assets are below).

环境/应用程序:使用Cordova / Ionic构建移动应用程序。仅支持iOS(webkit css)。

Environment/Application: Using Cordova/Ionic to build mobile application. Only supporting iOS (webkit css).

是否有任何组件允许我实现此目的?

Are there any components out there that allow me to achieve this?

图片:



推荐答案

这可能给你一些关于如何实现微调器的想法。使用Opera,Firefox和Chrome进行Android测试。

This might give you some an idea of how it is possible to implement a spinner. Tested with Opera, Firefox and Chrome for Android.

var NUM_HEIGHT = 55;
var Y_OFFSET = 50;
var SPINNER_HEIGHT = 150;

var targetPosition = 0;
var currentPosition = 0;
var deltaY = 0;

function targetReached() {
  deltaY = 0;
  currentPosition = targetPosition;
  document.getElementById("value").innerHTML = "Value: " + currentPosition;
}

function move() {
  var yPosition = -currentPosition * NUM_HEIGHT + deltaY + Y_OFFSET;
  document.getElementById("number").style.backgroundPosition = "0px " + yPosition + "px";

  if (targetPosition > currentPosition) {
    if (deltaY > -NUM_HEIGHT) {
      deltaY = deltaY - 5;
      setTimeout(move, 10);
    } else {
      targetReached();
    }
  } else if (targetPosition < currentPosition) {
    if (deltaY < NUM_HEIGHT) {
      deltaY = deltaY + 5;
      setTimeout(move, 10);
    } else {
      targetReached();
    }
  }
}
move();




function getClickPosition(e) {
  // Click position handling.
  // xPosition and yPosition are relative to element bounds.
  // Source: http://www.kirupa.com/html5/getting_mouse_click_position.htm
  var parentPosition = getPosition(e.currentTarget);
  var xPosition = e.clientX - parentPosition.x;
  var yPosition = e.clientY - parentPosition.y;

  if (yPosition > SPINNER_HEIGHT / 2 && currentPosition != 10) {
    targetPosition = currentPosition + 1;
  } else if (yPosition < SPINNER_HEIGHT / 2 && currentPosition != 0) {
    targetPosition = currentPosition - 1;
  }
  move();
}

function getPosition(element) {
  // Helper function
  // Source: http://www.kirupa.com/html5/getting_mouse_click_position.htm
  var xPosition = 0;
  var yPosition = 0;
  while (element) {
    xPosition += (element.offsetLeft - element.scrollLeft + element.clientLeft);
    yPosition += (element.offsetTop - element.scrollTop + element.clientTop);
    element = element.offsetParent;
  }
  return {
    x: xPosition,
    y: yPosition
  };
}

// document.getElementById("number").addEventListener("click", getClickPosition, false);
document.getElementById("number").addEventListener("mousedown", getClickPosition, false);

#spinner {
  background: url(http://i.stack.imgur.com/0Fc85m.png);
  background-size: 100% 100%;
  background-repeat: no-repeat;
  height: 150px;
  width: 300px;
}
#number {
  background: url(http://i.stack.imgur.com/c0ufam.png);
  background-repeat: no-repeat;
  background-size: 100% 600px;
  height: 150px;
  width: 50px;
  margin-left: 20px;
}

<div id="spinner">
  <div id="number">
  </div>
</div>
<br />
<div id="value">Value: 0</div>

这篇关于如何用图像创建JavaScript / HTML5 Spinner列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆