Write an ADHD resume using native JS

Preview address source address

Recently, I saw that @Fangyinghang wrote a moving resume with VUE on Zhihu. I thought it was quite fun, so I studied its implementation ideas and decided to try using native JS to achieve it.

Dynamic resume implementation ideas

This moving resume is like a typist constantly typing words, and the page appears to move. It’s like a movie has already been recorded and we’re just sitting in front of the projector watching it.

The principle has two parts

  1. The page sees the pulsing increment of text, controlled by innerHTML
  2. The layout effect of the page is changed by the hidden “behind”styleLabel to complete

Imagine you want to add one page to a web page every 0.1 secondahStart a timer and plug it into the body intermittentlyah, can ah! Yes, that completes the first part of the principle

Now imagine stuffing pagesahWhen I want to change the font color of the word and the background color of the web page, how should I do that? Is it ok to execute the following code? Yes, only change the font and background color is not a sudden change, but is also a timer, intermittent tostyleInsert the following code into the tag, which completes the second step of the principle, isn’t it easyLet’s do it step by step

.xxx{
  color: blue;
  background: red; 
}

Copy the code

VQuery (encapsulated DOM manipulation module)

Since the rest of the modules depend on this small module, let’s take a quick look at it first.

class Vquery {
  constructor (selector.context) {
    this.elements = getEles(selector, context)
  }

  optimizeCb (callback) {
    .
  }

  get (index) {
    .
  }

  html (sHtml) {
    .
  }

  addClass (iClass) {
    .
  }

  css (styles) {
    .
  }

  height (h) {
    .
  }

  scrollTop (top) {
    .}}export default (selector.context) = > {
  return new Vquery(selector, context)
}

Copy the code

As you can see, what it does is encapsulate a constructor, Vquery, whose instance will have some simple DOM manipulation methods. Finally, in order to be able to use $().funcname like jQuery, we export an anonymous function, in which we go to new Vquery

StylesEditor (stylesheet)

The resume editing module is used to display the content of the resume, mainly through the conversion from markdown format to HTML page form.

const markdownToHtml = (callback) => { $resumeMarkdown.css({ display: 'None'}) $resumewrap.addClass (iClass) $resumetag.html(marked(resumeMarkdown) Callback () // perform subsequent callback} const showResume = (callback) => { Turn off the timer when it's all plugged in, ClearInterval (timer) timer = setInterval(() => {currentMarkdown += resumemarkDown. substring(start, start + 1) if (currentMarkdown.length === length) { clearInterval(timer) callback && callback() } else { $resumeMarkdown.html(currentMarkdown) start++ } }, delay) }Copy the code

Resolve the promise of callback hell

The callback method can solve the problem of asynchronous operation, but the code is very ugly to write, poor readability, the code is horizontal development trend… Great programmers spread the boundaries and invented Promise’s solution. Let’s take a look at how the app module in the Promise branch ends up

showStylesWrap(0).then(showResumeWrap)
  .then(showStylesWrap.bind(null.1)).then(markdownToHtmlWrap)
  .then(showStylesWrap.bind(null.2))
Copy the code

As you can see, the code is much cleaner, vertically, applying step 1, Step 2, step 3… At a glance, you can see that, of course, the implementation logic is to wrap the original related module with Promise, and resolve in the original callback function execution place, detailed implementation, welcome to check the project source code

Resolve async in callback hell

Async is a syntactic sugar for generator functions. If you understand generator, you will not be able to use it. For details on the use of this project, please refer to the async branch source code.