Welcome! Please hold on...

0 %
Kashif Sohail
Sr. Frontend Developer
  • Residence:
    Pakistan
  • City:
    Karachi
  • Age:
    26
Magento 1x 2x
Wordpress
Laravel
Angular
React
English
Urdu
  • Bootstrap, Tailwind
  • Sass, Less, Css
  • Webpack
  • GIT knowledge
0

No products in the cart.

Pop(over) The Balloons | CSS-Tricks

August 13, 2024

[ad_1]

I’ve always been fascinated by how much we can achieve with HTML and CSS alone. The new interactive features of the Popover API are another example of how far we can go with just these two languages.

You may have seen other tutorials showing what the Popover API can do, but this is more of an article that mercilessly beats it into submission. We’ll add a little more pop Music to the mix, like balloons… a literal “pop,” if you will.

I’ve created a game – using only HTML and CSS, of course – based on the Popover API. Your task is to pop as many balloons as possible in less than a minute. But be careful! Some balloons are (as Gollum would say) “tricky” and will trigger more balloons.

I called it smart Let the balloons burst and we will do it together step by step. When we are done, it will look something like this (OK, Exactly like this:

Dealing with popover attribute

Any element can be a popover as long as we use the popover Attribute:

...

We don’t even have to deliver popover with a value. By default popoverThe initial value is auto and uses what the spec calls “light dismiss.” This means that the popover can be closed by clicking anywhere outside of it. And when the popover opens, all other popovers on the page are closed unless they are nested. Automatic popovers are interdependent in this regard.

The other possibility is popover to a manual Value:

...

…which means that the element is opened and closed manually – we literally have to click a specific button to open and close it. In other words, manual creates a stubborn popup that only closes when you press the right button and is completely independent of other popups on the page.

With the help of

Element as starter

One of the challenges of building a game with the Popover API is that you can’t load a page if a popover is already open. And there’s no way around that with JavaScript if our goal is to build the game using only HTML and CSS.

Enter the

Element. Unlike a popover, the

Element can be open by default:

If we follow this path, we can display a series of buttons (balloons) and “pop” them all until the very last balloon by pressing the

In other words, we can launch our balloons into an open

element so that they appear on the page when it loads.

I am talking about the basic structure:

This way we can click on the balloon in

to close the

and pop all the button balloons so that only one balloon remains (the

at the end (how to remove it, we’ll learn a little later).

You might think that

would be a more semantic direction for our game, and you would be right. But there are two disadvantages with that doesn’t let us use it here:

  1. The only way to that opens when the page loads is done with JavaScript. As far as I know, there is no way to
  2. s are modal and prevent clicking on other things while they are open. We need to allow players to pop balloons outside of the to beat the timer.

We therefore use a open> Element as the top-level container of the game and with a simple

for the popups themselves, i.e.

.

All we need to do for now is make sure that all of these popovers and buttons are connected together so that when a button is clicked, a popover opens. You've probably learned this already in other tutorials, but we need to tell the popover element that there is a button it needs to respond to, and then tell the button that there is a popup it needs to open. To do this, we give the popover element a unique ID (as all IDs should be) and then reference the

Level 1 Popup

This is the idea when everything is wired together:

Opening and closing popovers

There is still a little work to be done in this final demo. One of the drawbacks of the game so far is that clicking on the

Level 1 Popup

Note that I added a new

[ad_2]

Source link

Posted in TechnologyTags:
Write a comment