index.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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="text/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>
  29. <div id="app" @touchmove.prevent>
  30. <draggable-header-view>
  31. <template slot="header">
  32. <h1>Elastic Draggable SVG Header</h1>
  33. <p>with <a href="http://vuejs.org">Vue.js</a> + <a href="http://dynamicsjs.com">dynamics.js</a></p>
  34. </template>
  35. <template slot="content">
  36. <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>
  37. </template>
  38. </draggable-header-view>
  39. </div>
  40. <script>
  41. Vue.component('draggable-header-view', {
  42. template: '#header-view-template',
  43. data: function () {
  44. return {
  45. dragging: false,
  46. // quadratic bezier control point
  47. c: { x: 160, y: 160 },
  48. // record drag start point
  49. start: { x: 0, y: 0 }
  50. }
  51. },
  52. computed: {
  53. headerPath: function () {
  54. return 'M0,0 L320,0 320,160' +
  55. 'Q' + this.c.x + ',' + this.c.y +
  56. ' 0,160'
  57. },
  58. contentPosition: function () {
  59. var dy = this.c.y - 160
  60. var dampen = dy > 0 ? 2 : 4
  61. return {
  62. transform: 'translate3d(0,' + dy / dampen + 'px,0)'
  63. }
  64. }
  65. },
  66. methods: {
  67. startDrag: function (e) {
  68. e = e.changedTouches ? e.changedTouches[0] : e
  69. this.dragging = true
  70. this.start.x = e.pageX
  71. this.start.y = e.pageY
  72. },
  73. onDrag: function (e) {
  74. e = e.changedTouches ? e.changedTouches[0] : e
  75. if (this.dragging) {
  76. this.c.x = 160 + (e.pageX - this.start.x)
  77. // dampen vertical drag by a factor
  78. var dy = e.pageY - this.start.y
  79. var dampen = dy > 0 ? 1.5 : 4
  80. this.c.y = 160 + dy / dampen
  81. }
  82. },
  83. stopDrag: function () {
  84. if (this.dragging) {
  85. this.dragging = false
  86. dynamics.animate(this.c, {
  87. x: 160,
  88. y: 160
  89. }, {
  90. type: dynamics.spring,
  91. duration: 700,
  92. friction: 280
  93. })
  94. }
  95. }
  96. }
  97. })
  98. new Vue({ el: '#app' })
  99. </script>
  100. </body>
  101. </html>