index.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
  6. <title></title>
  7. <script src="../../dist/vue.js"></script>
  8. <script src="http://dynamicsjs.com/lib/dynamics.js"></script>
  9. <link rel="stylesheet" href="style.css">
  10. <!-- template for the component -->
  11. <script type="x/template" id="header-view-template">
  12. <div class="draggable-header-view"
  13. @mousedown="startDrag" @touchstart="startDrag"
  14. @mousemove="onDrag" @touchmove="onDrag"
  15. @mouseup="stopDrag" @touchend="stopDrag" @mouseleave="stopDrag">
  16. <svg class="bg" width="320" height="560">
  17. <path :d="headerPath" fill="#3F51B5"></path>
  18. </svg>
  19. <div class="header">
  20. <slot name="header"></slot>
  21. </div>
  22. <div class="content" :style="contentPosition">
  23. <slot name="content"></slot>
  24. </div>
  25. </div>
  26. </script>
  27. </head>
  28. <body @touchmove.prevent>
  29. <draggable-header-view>
  30. <template slot="header">
  31. <h1>Elastic Draggable SVG Header</h1>
  32. <p>with <a href="http://vuejs.org">Vue.js</a> + <a href="http://dynamicsjs.com">dynamics.js</a></p>
  33. </template>
  34. <template slot="content">
  35. <p>Note this is just an effect demo - there are of course many additional details if you want to use this in production, e.g. handling responsive sizes, reload threshold and content scrolling. Those are out of scope for this quick little hack. However, the idea is that you can hide them as internal details of a Vue.js component and expose a simple Web-Component-like interface.</p>
  36. </template>
  37. </draggable-header-view>
  38. <script>
  39. Vue.component('draggable-header-view', {
  40. template: '#header-view-template',
  41. data: function () {
  42. return {
  43. dragging: false,
  44. // quadratic bezier control point
  45. c: { x: 160, y: 160 },
  46. // record drag start point
  47. start: { x: 0, y: 0 }
  48. }
  49. },
  50. computed: {
  51. headerPath: function () {
  52. return 'M0,0 L320,0 320,160' +
  53. 'Q' + this.c.x + ',' + this.c.y +
  54. ' 0,160'
  55. },
  56. contentPosition: function () {
  57. var dy = this.c.y - 160
  58. var dampen = dy > 0 ? 2 : 4
  59. return {
  60. transform: 'translate3d(0,' + dy / dampen + 'px,0)'
  61. }
  62. }
  63. },
  64. methods: {
  65. startDrag: function (e) {
  66. e = e.changedTouches ? e.changedTouches[0] : e
  67. this.dragging = true
  68. this.start.x = e.pageX
  69. this.start.y = e.pageY
  70. },
  71. onDrag: function (e) {
  72. e = e.changedTouches ? e.changedTouches[0] : e
  73. if (this.dragging) {
  74. this.c.x = 160 + (e.pageX - this.start.x)
  75. // dampen vertical drag by a factor
  76. var dy = e.pageY - this.start.y
  77. var dampen = dy > 0 ? 1.5 : 4
  78. this.c.y = 160 + dy / dampen
  79. }
  80. },
  81. stopDrag: function () {
  82. if (this.dragging) {
  83. this.dragging = false
  84. dynamics.animate(this.c, {
  85. x: 160,
  86. y: 160
  87. }, {
  88. type: dynamics.spring,
  89. duration: 700,
  90. friction: 280
  91. })
  92. }
  93. }
  94. }
  95. })
  96. new Vue({ el: 'body' })
  97. </script>
  98. </body>
  99. </html>