You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

script-custom_elements.js 6.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. const svgNS = "http://www.w3.org/2000/svg"; //svg用的命名空间
  2. // Create a class for the element
  3. class CardAvatar extends HTMLElement {
  4. // Specify observed attributes so that
  5. // attributeChangedCallback will work
  6. static get observedAttributes() {
  7. return ['iid'];
  8. }
  9. #iid = 0;
  10. get iid() { this.#iid; }
  11. /**
  12. * @param {string | number} x
  13. */
  14. set iid(x) {
  15. //this.#iid = x; //在属性改变的内容里已经写入了
  16. this.setAttribute('iid', x);
  17. }
  18. //#member = new Member();
  19. /**
  20. * @param {Member} m
  21. */
  22. /*get member() {
  23. return this.#member;
  24. }*/
  25. /**
  26. * @param {Member} m
  27. */
  28. /*set member(m) {
  29. this.#member = m;
  30. console.log("设定新的Member",m);
  31. this.setAttribute('id', m.id);
  32. }*/
  33. constructor() {
  34. // Always call super first in constructor
  35. super();
  36. // Create a shadow root
  37. const shadow = this.attachShadow({mode: 'open'});
  38. // Create some CSS to apply to the shadow dom
  39. const linkElem = shadow.appendChild(document.createElement('link'));
  40. linkElem.setAttribute('rel', 'stylesheet');
  41. linkElem.setAttribute('href', 'css/card-avatar.css');
  42. // Create spans
  43. const wrapper = shadow.appendChild(document.createElement('a'));
  44. wrapper.className = 'wrapper';
  45. wrapper.target = '_blank';
  46. const dAttr1 = wrapper.appendChild(document.createElement('div'));
  47. dAttr1.className = 'attribute attribute-main';
  48. const dAttr2 = wrapper.appendChild(document.createElement('div'));
  49. dAttr2.className = 'attribute attribute-sub';
  50. const dLeftTop = wrapper.appendChild(document.createElement('div'));
  51. dLeftTop.className = "flex-box flex-left-top";
  52. const dLeftBottom = wrapper.appendChild(document.createElement('div'));
  53. dLeftBottom.className = "flex-box flex-left-bottom";
  54. const dRightTop = wrapper.appendChild(document.createElement('div'));
  55. dRightTop.className = "flex-box flex-right-top";
  56. const dRightBottom = wrapper.appendChild(document.createElement('div'));
  57. dRightBottom.className = "flex-box flex-right-bottom";
  58. const dId = dLeftBottom.appendChild(document.createElement('div'));
  59. dId.className = 'card-id';
  60. const dLevel = dLeftBottom.appendChild(document.createElement('div'));
  61. dLevel.className = 'level';
  62. dLevel.textContent = "110";
  63. const dEnhancement = dLeftTop.appendChild(document.createElement('div'));
  64. dEnhancement.className = 'enhancement';
  65. dEnhancement.textContent = "297";
  66. const dActiveSkillCD = dRightBottom.appendChild(document.createElement('div'));
  67. dActiveSkillCD.className = 'active-skill-cd';
  68. dActiveSkillCD.textContent = "99";
  69. }
  70. connectedCallback() {
  71. console.log('自定义标签添加到页面');
  72. this.update();
  73. }
  74. attributeChangedCallback(name, oldValue, newValue) {
  75. console.log('自定义标签属性改变', name, oldValue, newValue);
  76. //if (name == 'id') this.#id = parseInt(this.getAttribute('id'));
  77. if (name == 'iid') this.#iid = parseInt(newValue);
  78. this.update();
  79. }
  80. update() {
  81. //得到怪物ID
  82. const id = this.#iid || 0;
  83. this.iid = id;
  84. const card = Cards[id] || Cards[0];
  85. const dataSource = this.getAttribute('source') || currentDataSource.code;
  86. const shadow = this.shadowRoot;
  87. const wrapper = shadow.querySelector('.wrapper');
  88. wrapper.href = currentLanguage.guideURL(id, card.name);
  89. wrapper.style.backgroundImage = `url(images/cards_${dataSource.toLowerCase()}/CARDS_${Math.ceil(id / 100).toString().padStart(3,"0")}.PNG)`;
  90. const idxInPage = (id - 1) % 100; //获取当前页面内的序号
  91. wrapper.setAttribute("data-cards-pic-x", idxInPage % 10); //添加X方向序号
  92. wrapper.setAttribute("data-cards-pic-y", Math.floor(idxInPage / 10)); //添加Y方向序号
  93. const dAttr1 = wrapper.querySelector('.attribute-main');
  94. dAttr1.setAttribute('data-attr', card.attrs[0]);
  95. const dAttr2 = wrapper.querySelector('.attribute-sub');
  96. dAttr2.setAttribute('data-attr', card.attrs[1]);
  97. const dId = wrapper.querySelector('.card-id');
  98. dId.setAttribute('data-id', id);
  99. }
  100. }
  101. // Define the new element
  102. customElements.define('card-avatar', CardAvatar);
  103. class PadIcon extends HTMLElement {
  104. // Specify observed attributes so that
  105. // attributeChangedCallback will work
  106. static get observedAttributes() {
  107. return ['iid','type','lang'];
  108. }
  109. #iid = 0;
  110. #type = "awoken";
  111. get iid() { this.#iid; }
  112. /**
  113. * @param {string | number} x
  114. */
  115. set iid(x) {
  116. this.setAttribute('iid', x);
  117. this.#iid = x;
  118. }
  119. get type() { this.#type; }
  120. /**
  121. * @param {string} x
  122. */
  123. set type(x) {
  124. this.setAttribute('type', x);
  125. this.#type = x;
  126. }
  127. constructor() {
  128. // Always call super first in constructor
  129. super();
  130. // Create a shadow root
  131. const shadow = this.attachShadow({mode: 'open'});
  132. // Create some CSS to apply to the shadow dom
  133. const linkElem = shadow.appendChild(document.createElement('link'));
  134. linkElem.setAttribute('rel', 'stylesheet');
  135. linkElem.setAttribute('href', 'css/svg-icon.css');
  136. const svg = document.implementation.createDocument(svgNS,'svg');
  137. const use = svg.createElementNS(svgNS, 'use');
  138. svg.documentElement.appendChild(use);
  139. shadow.appendChild(svg.documentElement);
  140. }
  141. connectedCallback() { //自定义标签添加到页面
  142. this.update();
  143. }
  144. attributeChangedCallback(name, oldValue, newValue) { //自定义标签属性改变
  145. if (name == 'iid') this.#iid = parseInt(newValue);
  146. if (name == 'type') this.#type = newValue;
  147. this.update();
  148. }
  149. update() {
  150. let iid = this.#iid || 0;
  151. const type = this.#type;
  152. const lang = this.getAttribute('lang') || currentLanguage.i18n;
  153. const shadow = this.shadowRoot;
  154. const use = shadow.querySelector('use');
  155. switch (type) {
  156. case 'awoken': {
  157. if (/^(?:en|ko)/.test(lang) && [40,46,47,48].includes(iid)) iid += '-en'; //英文不一样的觉醒
  158. if (/^(?:zh)/.test(lang) && [46,47].includes(iid)) iid += '-zh'; //中文不一样的觉醒
  159. use.setAttribute("href",`images/icon-awoken.svg#awoken-${iid}`);
  160. break;
  161. }
  162. case 'type':
  163. case 'latent':
  164. case 'badge':
  165. case 'attr':
  166. case 'orb':
  167. case 'common':
  168. case 'skill':
  169. default:
  170. break;
  171. }
  172. }
  173. }
  174. // Define the new element
  175. customElements.define('pad-icon', PadIcon);

智龙迷城队伍图制作工具