tooltips.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <template>
  2. <view>
  3. <view id="just-tooltip"
  4. class="just-tooltip animated flipInBottom"
  5. :style="{width: 'auto', left: left+'px',top: top+'px',backgroundColor:backgroundColor,color:textColor}">
  6. <view class="just-con">
  7. <text class="item"
  8. :style="{borderColor:splitColor}"
  9. v-for="(item,index) in btns" :key="index" @click="btnClick(item)">{{ item }}</text>
  10. </view>
  11. <view :class="justClass" :style="justStyleObject"></view>
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. export default {
  17. name: 'BianmarenTooltip',
  18. props: {
  19. //是否显示
  20. tooltipShow:{
  21. type: Boolean,
  22. default:false
  23. },
  24. //颜色
  25. textColor:{
  26. type: String,
  27. default: "#ffffff"
  28. },
  29. //背景颜色
  30. backgroundColor:{
  31. type: String,
  32. default: "#000000"
  33. },
  34. //分隔符颜色
  35. splitColor:{
  36. type: String,
  37. default: "#ffffff"
  38. },
  39. //按钮组
  40. btns: {
  41. type: Array,
  42. default: ['HelloWorld',"你好啊"]
  43. },
  44. //点击元素Id
  45. eleId: {
  46. type: String,
  47. default: ""
  48. },
  49. //方向 bottom,top,left,right
  50. gravity:{
  51. type:String,
  52. default:"bottom"
  53. },
  54. distance:{
  55. type:Number,
  56. default:10
  57. }
  58. },
  59. data() {
  60. return {
  61. left:-9999,
  62. top:0,
  63. justStyleObject:{}
  64. };
  65. },
  66. watch:{
  67. eleId(){
  68. this.setPosition();
  69. },
  70. tooltipShow(){
  71. this.setPosition();
  72. }
  73. },
  74. mounted() {
  75. this.setPosition();
  76. },
  77. computed:{
  78. justClass(){
  79. return "just-"+this.gravity;
  80. }
  81. },
  82. methods:{
  83. setPosition(){
  84. var _this = this;
  85. if(!_this.tooltipShow){
  86. _this.left = -9999;
  87. return;
  88. }
  89. const query = uni.createSelectorQuery().in(_this.$parent);
  90. query.select('#'+_this.eleId).boundingClientRect(data => {
  91. var pos = { x: data.left, y: data.top };
  92. var wh = { w: data.width, h: data.height };
  93. var outerWidth = 0;
  94. var outerHeight = 0;
  95. const query2 = uni.createSelectorQuery().in(_this);
  96. query2.select('#just-tooltip').boundingClientRect(dd => {
  97. outerWidth = dd.width;
  98. outerHeight = dd.height;
  99. const systemInfo = uni.getSystemInfoSync();
  100. var windowWidth = systemInfo.windowWidth;
  101. var rightTmp = ( pos.x + wh.w / 2 ) + outerWidth / 2 ;
  102. var leftTmp = ( pos.x + wh.w / 2 ) - outerWidth / 2 ;
  103. if("top" === _this.gravity){
  104. if(rightTmp > windowWidth ){
  105. _this.left = pos.x + wh.w - outerWidth;
  106. _this.top = pos.y - outerHeight - _this.distance;
  107. _this.justStyleObject = {
  108. 'left':(outerWidth - wh.w/2) + "px",
  109. 'borderColor':_this.backgroundColor+" transparent transparent transparent"
  110. };
  111. }else if( leftTmp < 0 ){
  112. _this.left = pos.x;
  113. _this.top = pos.y - outerHeight - _this.distance;
  114. _this.justStyleObject = {
  115. 'left':wh.w/2 + "px",
  116. 'borderColor':_this.backgroundColor+" transparent transparent transparent"
  117. };
  118. }else{
  119. _this.left = pos.x - (outerWidth - wh.w)/2;
  120. _this.top = pos.y - outerHeight - _this.distance
  121. _this.justStyleObject = {
  122. 'left':"50%",
  123. 'borderColor':_this.backgroundColor+" transparent transparent transparent"
  124. };
  125. }
  126. }
  127. if('bottom' === _this.gravity){
  128. if(rightTmp > windowWidth ){
  129. _this.left = pos.x + wh.w - outerWidth;
  130. _this.top = pos.y + wh.h + _this.distance;
  131. _this.justStyleObject = {
  132. 'left':(outerWidth - wh.w/2)+'px',
  133. 'borderColor':"transparent transparent "+_this.backgroundColor+" transparent"
  134. };
  135. }else if( leftTmp < 0 ){
  136. _this.left = pos.x;
  137. _this.top = pos.y + wh.h + _this.distance;
  138. _this.justStyleObject = {
  139. left:wh.w/2 + 'px',
  140. 'borderColor':"transparent transparent "+_this.backgroundColor+" transparent"
  141. }
  142. }else{
  143. _this.left = pos.x - (outerWidth - wh.w)/2;
  144. _this.top = pos.y + wh.h + _this.distance;
  145. _this.justStyleObject = {
  146. left:'50%',
  147. 'borderColor':"transparent transparent "+_this.backgroundColor+" transparent"
  148. }
  149. }
  150. }
  151. if('left' === _this.gravity){
  152. _this.left = pos.x - outerWidth - _this.distance;
  153. _this.top = pos.y - (outerHeight - wh.h)/2;
  154. _this.justStyleObject = {
  155. top:'50%',
  156. 'borderColor':"transparent transparent transparent " + _this.backgroundColor
  157. }
  158. }
  159. if('right' === _this.gravity){
  160. _this.left = pos.x + wh.w + _this.distance;
  161. _this.top = pos.y - (outerHeight - wh.h)/2;
  162. _this.justStyleObject = {
  163. top:'50%',
  164. 'borderColor':"transparent "+_this.backgroundColor+" transparent transparent"
  165. }
  166. }
  167. }).exec();
  168. }).exec();
  169. },
  170. //按钮点击
  171. btnClick(btnName){
  172. this.$emit('btnClick',btnName)
  173. }
  174. }
  175. }
  176. </script>
  177. <style>
  178. /* just-Tips */
  179. .just-tooltip{position:absolute;left:0;top:0;border-radius:5px;background:#000;/*display:inline-block;*/z-index:9999;}
  180. .just-tooltip .just-con{padding:8px 10px;}
  181. .just-tooltip .just-top,
  182. .just-tooltip .just-bottom,
  183. .just-tooltip .just-left,
  184. .just-tooltip .just-right{content:"";position:absolute;width:0;height:0;overflow:hidden;border-style:solid;}
  185. .just-tooltip .just-top{left:50%;top:100%;border-width: 7px 5px 0 5px;margin-left:-5px;
  186. border-color: #1B1E24 transparent transparent transparent;
  187. _border-color: #1B1E24 #000000 #000000 #000000;
  188. _filter: progid:DXImageTransform.Microsoft.Chroma(color='#000000');
  189. }
  190. .just-tooltip .just-bottom{ left:50%; top:-7px;border-width: 0 5px 7px 5px;margin-left:-5px;
  191. border-color: transparent transparent #1B1E24 transparent;
  192. _border-color:#000000 #000000 #1B1E24 #000000;
  193. _filter: progid:DXImageTransform.Microsoft.Chroma(color='#000000');}
  194. .just-tooltip .just-left{ right:-7px; top:50%;border-width: 5px 0 5px 7px;margin-top:-5px;;
  195. border-color: transparent transparent transparent #1B1E24;
  196. _border-color:#000000 #000000 #000000 #1B1E24;
  197. _filter: progid:DXImageTransform.Microsoft.Chroma(color='#000000');
  198. }
  199. .just-tooltip .just-right{ left:-7px; top:50%;border-width: 5px 7px 5px 0;margin-top:-5px;
  200. border-color: transparent #1B1E24 transparent transparent;
  201. _border-color:#000000 #000000 #000000 #1B1E24;
  202. _filter: progid:DXImageTransform.Microsoft.Chroma(color='#000000');
  203. }
  204. .just-tooltip .just-confirm{text-align:center;padding:10px 0;margin:0 10px 10px 10px;}
  205. .just-tooltip .just-yes, .just-tooltip .just-no{background:#fff;color:#000;border:0;padding:5px 10px;}
  206. .just-tooltip .just-no{margin-left:10px;}
  207. /* Animations */
  208. .animated{
  209. -webkit-animation-fill-mode:both;-moz-animation-fill-mode:both;-ms-animation-fill-mode:both;-o-animation-fill-mode:both;animation-fill-mode:both;
  210. -webkit-animation-duration:.5s;-moz-animation-duration:.5s;-ms-animation-duration:.5s;-o-animation-duration:.5s;animation-duration:.5s;
  211. }
  212. @-webkit-keyframes flipInUp {
  213. 0% { -webkit-transform: perspective(400px) rotateX(-90deg); opacity: 0;}
  214. 40% { -webkit-transform: perspective(400px) rotateX(5deg);}
  215. 70% { -webkit-transform: perspective(400px) rotateX(-5deg);}
  216. 100% { -webkit-transform: perspective(400px) rotateX(0deg); opacity: 1;}
  217. }
  218. @-moz-keyframes flipInUp {
  219. 0% {-moz-transform: perspective(400px) rotateX(-90deg);opacity: 0;}
  220. 40% {-moz-transform: perspective(400px) rotateX(5deg);}
  221. 70% {-moz-transform: perspective(400px) rotateX(-5deg);}
  222. 100% {-moz-transform: perspective(400px) rotateX(0deg);opacity: 1;}
  223. }
  224. @-o-keyframes flipInUp {
  225. 0% {-o-transform: perspective(400px) rotateX(-90deg);opacity: 0;}
  226. 40% {-o-transform: perspective(400px) rotateX(5deg);}
  227. 70% {-o-transform: perspective(400px) rotateX(-5deg);}
  228. 100% {-o-transform: perspective(400px) rotateX(0deg);opacity: 1;}
  229. }
  230. @keyframes flipInUp {
  231. 0% {transform: perspective(400px) rotateX(-90deg);opacity: 0;}
  232. 40% {transform: perspective(400px) rotateX(5deg);}
  233. 70% {transform: perspective(400px) rotateX(-5deg);}
  234. 100% {transform: perspective(400px) rotateX(0deg);opacity: 1;}
  235. }
  236. @-webkit-keyframes flipInRight {
  237. 0% { -webkit-transform: perspective(400px) rotateY(-90deg); opacity: 0;}
  238. 40% { -webkit-transform: perspective(400px) rotateY(5deg);}
  239. 70% { -webkit-transform: perspective(400px) rotateY(-5deg);}
  240. 100% { -webkit-transform: perspective(400px) rotateY(0deg); opacity: 1;}
  241. }
  242. @-moz-keyframes flipInRight {
  243. 0% {-moz-transform: perspective(400px) rotateY(-90deg);opacity: 0;}
  244. 40% {-moz-transform: perspective(400px) rotateY(5deg);}
  245. 70% {-moz-transform: perspective(400px) rotateY(-5deg);}
  246. 100% {-moz-transform: perspective(400px) rotateY(0deg);opacity: 1;}
  247. }
  248. @-o-keyframes flipInRight {
  249. 0% {-o-transform: perspective(400px) rotateY(-90deg);opacity: 0;}
  250. 40% {-o-transform: perspective(400px) rotateY(5deg);}
  251. 70% {-o-transform: perspective(400px) rotateY(-5deg);}
  252. 100% {-o-transform: perspective(400px) rotateY(0deg);opacity: 1;}
  253. }
  254. @keyframes flipInRight {
  255. 0% {transform: perspective(400px) rotateY(-90deg);opacity: 0;}
  256. 40% {transform: perspective(400px) rotateY(5deg);}
  257. 70% {transform: perspective(400px) rotateY(-5deg);}
  258. 100% {transform: perspective(400px) rotateY(0deg);opacity: 1;}
  259. }
  260. .flipInTop, .flipInBottom .flipInLeft .flipInRight { -webkit-backface-visibility: visible !important; -moz-backface-visibility: visible !important; -o-backface-visibility: visible !important; backface-visibility: visible !important}
  261. .flipInTop, .flipInBottom { -webkit-animation-name: flipInUp; -moz-animation-name: flipInUp; -o-animation-name: flipInUp; animation-name: flipInUp; }
  262. .flipInLeft, .flipInRight { -webkit-animation-name: flipInRight; -moz-animation-name: flipInRight; -o-animation-name: flipInRight; animation-name: flipInRight; }
  263. @-webkit-keyframes fadeIn { 0% {opacity: 0;} 100% {opacity: 1;}}
  264. @-moz-keyframes fadeIn { 0% {opacity: 0;} 100% {opacity: 1;}}
  265. @-o-keyframes fadeIn {0% {opacity: 0;}100% {opacity: 1;}}
  266. @keyframes fadeIn {0% {opacity: 0;}100% {opacity: 1;}}
  267. .fadeIn{-webkit-animation-name: fadeIn; -moz-animation-name: fadeIn; -o-animation-name: fadeIn; animation-name: fadeIn;}
  268. .moveTop{
  269. -webkit-animation: moveTop .6s ease both;
  270. animation: moveTop .6s ease both;
  271. }
  272. .moveBottom{
  273. -webkit-animation: moveBottom .6s ease both;
  274. animation: moveBottom .6s ease both;
  275. }
  276. .moveLeft{
  277. -webkit-animation: moveLeft .6s ease both;
  278. animation: moveLeft .6s ease both;
  279. }
  280. .moveRight{
  281. -webkit-animation: moveRight .6s ease both;
  282. animation: moveRight .6s ease both;
  283. }
  284. @-webkit-keyframes moveTop {
  285. from {opacity: 0;-webkit-transform: translateY(-20px);}
  286. to {opacity: 1;-webkit-transform: translateY(0); }
  287. }
  288. @-webkit-keyframes moveBottom {
  289. from {opacity: 0;-webkit-transform: translateY(20px);}
  290. to {opacity: 1;-webkit-transform: translateY(0); }
  291. }
  292. @-webkit-keyframes moveLeft {
  293. from {opacity: 0;-webkit-transform: translateX(-20px);}
  294. to {opacity: 1;-webkit-transform: translateX(0); }
  295. }
  296. @-webkit-keyframes moveRight {
  297. from {opacity: 0;-webkit-transform: translateX(20px);}
  298. to {opacity: 1;-webkit-transform: translateX(0); }
  299. }
  300. .item{
  301. display: inline-block;
  302. border-right: #fff solid 1px;
  303. padding: 0 20rpx;
  304. }
  305. .item:last-child{
  306. border: none;
  307. }
  308. </style>