Thursday, September 28, 2017

Conditionally Applying a CSS Class in Vue.js

There are times you need to change an element's CSS classes at runtime. But when changing classes, it's sometimes best to apply style details conditionally. For example, imagine your view has a pager. Pagers are often used to navigate larger sets of items. When navigating, it can be helpful to show the user the page they're currently on. The style of the item is conditionally set, based on the current page that's being viewed.

A pager in this case may look something like this:

Pager

In this example, there are five pages. Only one of these pages is selected at a time. If you built this pager with Bootstrap, the selected page would have a CSS class named active applied. You'd want this class applied only if the page was the currently viewed page. In other words, you'd want to conditionally apply the active CSS class. As discussed in my Vue.js tutorial, Vue provides a way to conditionally apply a CSS class to an element. I'm going to show you this technique in this article.

To conditionally apply a CSS class at runtime, you can bind to a JavaScript object. To successfully complete this task, you must complete two steps. First, you must ensure that your CSS class is defined. Then, you create the class bindings in your template. I'm going to explain each of these steps in detail in the rest of this article.

Step 1: Define Your CSS Classes

Imagine, for a moment, that the five page items shown in the image above were defined using the following HTML:

<div id="myApp">
  <nav aria-label="Page navigation example">
    <ul class="pagination">
      <li class="page-item"><a class="page-link" href="#">1</a></li>
      <li class="page-item"><a class="page-link" href="#">2</a></li>
      <li class="page-item active"><a class="page-link" href="#">3</a></li>
      <li class="page-item"><a class="page-link" href="#">4</a></li>
      <li class="page-item"><a class="page-link" href="#">5</a></li>
    </ul>
  </nav>
</div>

Notice that each page in this code snippet has a list item element (<li …). That element references the page-item CSS class. In the code for this article, this class is defined in the Bootstrap CSS framework. However, if it weren't defined there, it would be your responsibility to ensure that it was defined somewhere. The second CSS class is the one that's most relevant to this article, though.

The active CSS class is used to identify the currently selected page. For this article, this CSS class is also defined in the Bootstrap CSS. As shown in the snippet above, the active class is only used in the third list item element. As you can probably guess, this is the CSS class that you want to apply conditionally. To do that, you need to add a JavaScript object.

Continue reading %Conditionally Applying a CSS Class in Vue.js%


by Chad Campbell via SitePoint

120+ Places To Find Creative Commons Media

The number of images, audio files, movies and other files available under a Creative Commons license is enormous, as Sean demonstrates in this post. Check out his list of over 30 useful sites for sourcing Creative Commons media.

Continue reading %120+ Places To Find Creative Commons Media%


by Adrian Sandu via SitePoint

Learn Computer Science With JavaScript: Part 3, Loops

Introduction

Suppose you have been given the task to write a program that displays the numbers 1–100. One way you could accomplish this is to write 100 console.log statements. But I’m sure you wouldn’t because you would have become fed up by the 9th or 10th line.  

The only part that changes in each statement is the number, so there should be a way to write only one statement. And there is with loops. Loops let us perform a set of steps in a code block repeatedly.  

Contents

  • While loops
  • Do-while loops
  • For loops
  • Arrays
  • For-in loops
  • For-of loops
  • Review
  • Resources

While Loops

While loops will execute a set of statements repeatedly while some condition is true. When the condition is false, the program will exit the loop. This kind of loop tests the condition before performing an iteration. An iteration is an execution of the loop’s body. The following example will not display anything because our condition is false.

This is the general form of a while loop:

One thing to be careful of when using while loops is creating loops that never end. This happens because the condition never becomes false. If it happens to you, your program will crash. Example:

Task

How many times will the body of this loop be executed:

Do-While Loops

A do-while loop will execute the body of statements first, and then check the condition. This kind of loop is useful when you know you want to run the code at least once. The following example will display “eat” once, even though the condition is false.

This is the general form for a do while-loop:

Task

Write a do-while loop that will display the numbers 1–10.

For Loops

A for-loop will repeat execution of a code block for a specific number of times. The following example displays the numbers 1–10:

This is the general form of a for-loop:

Initial is an expression that sets the value of our variable. Condition is an expression that must be true for the statements to execute. And step is an expression that increments the value of our variable.

One programming pattern is to use a for loop to update the value of a variable with itself and a new value. This example sums the numbers 1–10:

The += is an assignment operator that adds a value back to a variable. This is a list of all the assignment operators:

Operator
Example
Equivalent
+= x += 2
 x = x + 2
-= x -= 2
x = x - 2
*= x *= 2
x = x * 2
/= x /= 2
x = x / 2
%= x %= 2
x = x % 2

Task 

Write a for loop that calculates the factorial of a number. The factor of a number n is the product of all the integers from 1 to n. For example, 4! (4 factorial) is 1 x 2 x 3 x 4 which equals 24.

Arrays

An array is an object that holds a list of items, called elements, which are accessed by their index. The index is the position of the element in the array. The first element is at the 0 index. The following are some common array operations.

Create an empty array:

Initialize an array with values:

Get an element from an array:

Update an element in an array:

Loop over an array:

A two-dimensional array is an array whose elements are arrays. Example:

This is how you would loop over the array and display each element:

Task 

What element is displayed when i = 1 and j = 0 in the above for loop?

For-In Loop

This kind of loop lets us loop through the keys in an object. An object is a data structure that has keys mapped to values. Here are some common operations that can be performed on an object.

Create an empty object:

Initialize an object with values:

Get a property from an object:

Update a property in an object:

Loop over the keys of an object:

Task

What does the above for loop display given obj = {foo: "Hello", bar: "World"}?

For-Of Loop

This kind of loop lets us loop over the values of iterable objects. Examples of iterable objects are arrays and strings.

Loop over an array:

Loop over a string:

Task

Using any of the loops, write a program that will display this staircase pattern:

Review

Loops let us reduce duplication in our code. While loops let us repeat an action until a condition is false. A do-while loop will execute at least once. For loops let us repeat an action until will we reach the end of a count. The for-in loop is designed so we can access the keys in an object. The for-of loop is designed so we can get the value of an iterable object. 

Next, in part 4, we will learn about functions.

Resources


by Alberta Williams via Envato Tuts+ Code

What Is a Bot? [video]

A brief introduction to bots, their history, and how they will impact your online life.

[ This is a content summary only. Visit our website http://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Web Desk via Digital Information World

How to Create and Use Instagram Collections

Do you save Instagram posts? Looking for a way to organize your saved posts? In this article, you’ll learn how to create private Instagram collections to organize saved posts you want to refer to later. Why Instagram Collections? When you save Instagram posts you want to refer to later on, they’re added to a private [...]

This post How to Create and Use Instagram Collections first appeared on .
- Your Guide to the Social Media Jungle


by Robert Katai via

G-Star Elwood Jeans

Drag and explore to find your own G-Star Elwood Jeans in this WebGL shopping experience packed with smooth transitions and sleek visual effects.
by via Awwwards - Sites of the day

10 Movies Every Entrepreneur Needs to Watch

No one ever said being an entrepreneur would be easy. A million obstacles seem to stand in the way each and every day. The naysayers and budget woes can be enough for the average person to start waving the white flag. But you are not an average person: You’re an entrepreneur. That means that even...

[ This is a content summary only. Visit our website http://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Web Desk via Digital Information World