I believe that every layout designer should solve this problem at least once in his life, and after that he should start collecting solutions for this task.
Of course, the solution to this task can be found in the Internet for five minutes conditional. And it may, for example, be like this: css-tricks.com/centering-css-complete-guide . Therefore, the implementation was important, in addition to the number of solutions, to also provide an explanation of where it is worth using. For example, we intentionally did not write that the dimensions of an element can be specified, and in some solutions this is acceptable. We also paid attention to such things as CodeStyle, the naming of classes or id'shnikov elements, the use of semantic tags and the layout layout in general (assembly, splitting into files).
Let's look at possible solutions. First let us know the width of the element. Then there are such options:
- margin-left and margin-right in auto. This is perhaps the most common use of recession mathematics of block elements.
- In the same piggy bank solution with the job position: absolute; left: 50%; margin-left: <half width>. And this is the second most popular way to deal with it.
- Modification of the first option for position: absolute blocks. In this decision, it is important to set not only automatic margins, but also left: 0; right: 0;
Solutions to center the element vertically with a given height are not very interesting to consider, since this is most likely a variation of the solutions horizontally. Therefore, further solutions for elements with unknown width and height:
- Put an element inside a table cell for which middle alignment is specified. Perhaps earlier, when there was IE6, it was the most bulletproof way.
- Take advantage of javascript. And that, too, is quite a decision.
- More modern solution. Set the inline-block display element and the parent to align the text in the center (text-align: center). And for vertical centering the pseudo-element :: before set the height to 100%, width 0, line-height 100% and vertical-align: middle.
- More modern solutions are the use of the CSS calc function.
- Or the peculiarities of the fact that the transform, given as a percentage, is calculated from the size of the element.
- And finally, flexboxes, and they can also be centered differently, because there are align-items, there is justify-content, and you can also change the direction of output of flex-blocks through flex-direction.
Finally, the third, my favorite group of solutions, are those that are found by chance or vice versa thanks to many years of research. These include the following.
Method Studio Lebedev . It was invented by Vladimir Tokmakov, and in 2008 it was practically the only way to solve the set task for an element with an arbitrary width.
Two more solutions were thrown to me by the students of the first SRI in Yekaterinburg.
The way Julia Gorshkova . She came up with the following: put an element in the middle cell of a table with three cells and stretched 100%, and set the width of the right and left columns to 50%. Knowing that the tables always try to fill the free space as much as possible, you can make the element burst the middle cell to the size you need.
Leonid Solomein way . This is my favorite option - you just need to put an item in.
But there is the most progressive way, which, unfortunately, is not supported by any browser yet. As soon as this changes, all 12 of these decisions will sink into oblivion. This is using position: center.
The topic of code optimization is very broad. There are many criteria that are worth paying attention to when evaluating the quality of a code, but several important ones can be highlighted:
- the presence of errors in behavior;
- readability, code uniformity;
- ease of understanding relationships;
- work speed;
- the presence of "bikes" when you can use ready-made working solutions.
The main problem in the task code was that with the specified addition of a callback to the animation, it is called as many times as the number of elements found by the selector. As a result, upon completion of the animation, many more run_equalizer are hung on the element, which erupts in memory like an avalanche, and the tab freezes very quickly. Also, in the above code, there was no caching, which only aggravated the problem. However, it is quite easy to notice, and in the end almost everyone coped with it. Therefore, in order to consider the problem solved, it is enough to correct the error with callbacks and add caching of elements.
But there were other ways to improve the code and impress the judges.
- Bring the naming of variables to a single style. It is very difficult to read the code, where there are no uniform rules of registration.
- Break the code into smaller methods. It is much easier to read a small, logically integral method, 5-10 lines long, than a “spaghetti code”.
- Reduce the number of timers. Working with animations of a set of elements, it is logical to use some kind of common animator with a single timer, which in one iteration will update everything at once.
- Run the code as early as possible . The code used the window.onload event, but it would be much more logical to use the DOMContentLoaded event or simply place an animation call under the block.
- Optimize CSS . The task animates the span (display: inline-block). It is tied to the size of the font, line, and other environments and affects the reflow / repaint of the entire page. Absolute animated elements will give a performance boost.
- Checkout code as an independent module. The word module in this context can be interpreted quite broadly: it could be a jQuery plugin or a separate “class”. For example, it is not immediately clear what the arguments mean when the function is called:
setEqualizer('#eq_1 .equalizer', 1000, 2);
. However, this is much better:$('#eq_1 .equalizer').equalizer({itemWidth: 2, animationDuration: 1000})
. Or like this:new Equalizer('#eq_1 .equalizer').setItemWidth(2).setAnimationDuration(1000).start()
.- Apply technology, more adapted to the animation: css, canvas, and even WebGL.
Working with the code, we spend much more time reading it than writing. Therefore, the ease of its reading and understanding is a very important parameter that must be taken into account when interacting with code.
The task, formulated in a rather laconic way, at first glance seems simple, but contains a very large scope for improving and polishing its solution. Let's get started
First of all, you should deal with what d3.js is . The first visit to the official library website shows that this is not just another wrapper over WebGL for 3d-graphics (what the name could hint at), but a very large set of tools for visualizing and animating various types of data. Well, not only the library turned out to be fundamentally new here, but the entire subject area as well. So the option to quickly review the API documentation of a new, but conceptually the same MVC framework will not work. We'll have to start by reading the tutorials.
From the introductory part it became clear that the library operates with data sets. In our case it will be a set of points, each of which is known for its color and coordinates. Yes, and since we are making a prototype about “working out the library,” it’s better to start using its capabilities to the maximum. For example,d3.range
instead of creating an array of points manually:
var dots = d3.range(1000); d3.select('svg').selectAll('circle') .enter().append('circle');
Immediately check that our points are normally displayed on the svg-canvas.
Now you can animate. The same lessons on the site, exactly like other articles about animation, speak of two quite important things:
- the animation should be built based on the timer events, and the changes should be calculated based on the elapsed time;
- complex animation should be decomposed into “layers”, in each of which one characteristic changes.
And for that, and for another in d3.js there are helper functions, and we will use them.
When checking the task, I first of all paid attention to the accuracy of the code: the consistency of the code-style, the naming of variables, functions and CSS selectors. At the same time, in those tasks where the subject was initially offered a low-quality code, this was especially important. If a person is disciplined, then writing neat code is simple. On the other hand, in order to clean up after others, you need to have a little perfectionist living in you who will not let you finish until everything is done as it should.
If the subject uses the modern capabilities of the browser to complete the task, then this is certainly a plus. But if he has not provided for polyfiles or folbacks to support all the declared browsers, then this is twice minus. You can not grab at everything new, even if it greatly simplifies your life. If your user suffers - you do something wrong.
When checking the JS code, it was important for me to see the quality code, both in terms of structure and algorithmic side. In general, I proceeded from the same principles that guide the code review. Would I like to see this code in my repository? Am I ready to support him? As a result, the most merciless selection. The downside was the mixing of paradigms and violation of the principles of DRY and KISS, errors identified by WebStorm / jshint, inconsistency of logic and approaches. A major mistake was the presence of errors in the developer tools console.
Using the BEM methodology was a plus when checking CSS. Also, the number and conciseness of the solutions found were a plus. I put the minuses only for the use of complex selectors and selectors by identifier.
In the optimization task, the main metric of success for me was the number of frames per second: 60 — excellent, 30 — good, and everything below is bad. I also put a minus, if a fan started working on my laptop, after starting the task, even if there was a necessary smoothness.
Source: https://habr.com/ru/post/264631/
All Articles