javascript - 如何让太阳的弧线停在页面的左端?

标签 javascript jquery html css draggable

我让太阳在弧形中可拖动,但我希望弧形停止在某个点,在本例中,该点是页面的左端。现在它永远在左边,但我想让它停下来。我该怎么做?

var width = 300;
var sun = $("#sun");

sun.draggable({
  axis: "x",
  drag: function() {
    var x = sun.offset().left + (width / 2);
    var total = $(window).width();

    var heightPct = Math.pow((total / 2) - x, 2) / Math.pow($(window).width() / 2, 2);
    console.log(x, $(window).width(), heightPct * 100);
    this.style["margin-top"] = "" + Math.round(heightPct * 30) + "%";
  }
});
/* Colors */

body {
  background: url(http://i.imgur.com/aZty7Mq.png);
  animation: mymove 4s linear infinite;
  -webkit-animation: mymove 4s linear infinite;
  -moz-animation: mymove 4s linear infinite;
}
@keyframes mymove {
  0% {
    background-position: 0 0;
  }
  50% {
    background-position: 40% 0;
  }
}
#dark_sun {
  position: absolute;
  width: 300px;
  height: 300px;
  top: 20%;
  left: 10%;
}
#sun {
  position: absolute;
  width: 300px;
  height: 300px;
  top: 20%;
  left: 10%;
}
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<img id="dark_sun" src="http://i.imgur.com/f3UFHb7.png">
<img id="sun" src="http://i.imgur.com/DGkZYZQ.png">

最佳答案

设置containment optionbody (或其他一些容器)在你的 draggable 定义中,并在你的 CSS 中隐藏 body 上的 x-overflow:

var width = 300;
var sun = $("#sun");

sun.draggable({
  axis: "x",
  containment: 'body',
  drag: function() {
    var x = sun.offset().left + (sun.width() / 2);
    var total = $(window).width();

    var heightPct = Math.pow((total / 2) - x, 2) / Math.pow($(window).width() / 2, 2);
    console.log(x, $(window).width(), heightPct * 100);
    this.style["margin-top"] = "" + Math.round(heightPct * 30) + "%";
  }
});
/* Colors */

body {
  background: url(http://i.imgur.com/aZty7Mq.png);
  padding: 30px;
  overflow-x: hidden;
  animation: mymove 4s linear infinite;
  -webkit-animation: mymove 4s linear infinite;
  -moz-animation: mymove 4s linear infinite;
}
@keyframes mymove {
  0% {
    background-position: 0 0;
  }
  50% {
    background-position: 40% 0;
  }
}
#dark_sun {
  position: absolute;
  width: 300px;
  height: 300px;
  top: 20%;
  left: 10%;
}
#sun {
  position: absolute;
  width: 300px;
  height: 300px;
  top: 20%;
  left: 10%;
}
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<img id="dark_sun" src="http://i.imgur.com/f3UFHb7.png">
<img id="sun" src="http://i.imgur.com/DGkZYZQ.png">

关于javascript - 如何让太阳的弧线停在页面的左端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33191236/

相关文章:

Jquery 对于每个最接近的值,计算并替换

html - 带有未在 VoiceOver 标题转子中列出的链接的标题

html - 在移动响应式设计中使用绝对位置

html - 溢出自动容器内的元素没有正确的宽度(所有组合的子元素)

javascript - 将 vtype 添加到网格 EXT-JS 中的列

javascript - 谷歌地图 API v3 : Dynamically update position on polyline direction

javascript - 如何在 JavaScript 中处理 DOM 元素以避免内存泄漏

Javascript 和 jquery 可与 IE 配合使用

javascript - 使用来自 API 的数据在 Angular 中创建动态复选框验证

javascript - Cordova 3.x 在运行时 IOS 强制屏幕方向