How can the <animate /> tag be triggered using only CSS and HTML?

Looking for a way to trigger the animation rules of the <animate /> tag using only HTML and CSS. Is there a new standard in HTML and CSS that allows something like

input:checked ~ svg animate {enabled:true;}
coming up in 2020? Or some other creative workaround without relying on JavaScript?

Challenging myself to avoid JavaScript completely in this project.

  <body>
    <input type="checkbox" />
    <svg class="curtain home" viewBox="0 0 100 100" preserveAspectRatio="none">
    <defs>
      <pattern id="img1" patternUnits="userSpaceOnUse" width="100" height="100">
    <image xlink:href="https://picsum.photos/id/1000/800/800" x="0" y="0" width="100" height="100" />
      </pattern>
    </defs>
    <polygon points="0,0 0,100 100,100 50,0" fill="url(#img1)">
    <animate attributeName="points" dur="5s" values="0,0 0,100 100,100 50,0 ; 50,40 30,60 80,10 50,70; 0,0 0,100 100,100 50,0" keyTimes="0;0.5;1" repeatCount="indefinite"/>
    </polygon>
    </svg> 
    </body>


ADDITIONAL

Exploring options to create the illusion of no animation until the input field is checked. Considering another HTML element that could interact well with the <animate /> tag to give the impression that animation starts and stops based on user interaction with the webpage.

Answer №1

One interesting technique is to implement a feature where you can switch between displaying two different polygons.

.hide {
  display:none;
}

input:checked~svg .hide {
  display:block;
}
input:checked~svg .show {
  display:none;
}
<body>
  <input type="checkbox" >
  <svg class="curtain home" viewBox="0 0 100 100" preserveAspectRatio="none">
        <defs>
          <pattern id="img1" patternUnits="userSpaceOnUse" width="100" height="100">
            <image xlink:href="https://picsum.photos/id/1000/800/800" x="0" y="0" width="100" height="100" />
          </pattern>
        </defs>
        <polygon points="0,0 0,100 100,100 50,0" fill="url(#img1)" class="show">
        </polygon>
        <polygon points="0,0 0,100 100,100 50,0" fill="url(#img1)" class="hide">
            <animate attributeName="points" dur="5s" values="0,0 0,100 100,100 50,0 ; 50,40 30,60 80,10 50,70; 0,0 0,100 100,100 50,0" keyTimes="0;0.5;1" repeatCount="indefinite"/>
        </polygon>
    </svg>
</body>

Another approach is to utilize the click event. By placing two rectangles on top of each other and using z-index in CSS, you can create an interactive experience:

Here is a simple example, but feel free to enhance the appearance of the red rectangle to make it more appealing as it will be the one users have to click on.

label {
  position:absolute;
  z-index:0;
}

input:checked + label {
 z-index:1;
}
<body>
  <input type="checkbox" id="input" >
  <label for="input"><svg viewBox="0 0 10 10" height="20">
        <rect id="stop" x=0 y=0 width="100%" height="100%" fill="red" />
  </svg></label>
  <label for="input"><svg viewBox="0 0 10 10" height="20">
        <rect id="start" x=0 y=0 width="100%" height="100%" fill="red" />
  </svg></label>

  <svg class="curtain home" viewBox="0 0 100 100" preserveAspectRatio="none">
        <defs>
          <pattern id="img1" patternUnits="userSpaceOnUse" width="100" height="100">
            <image xlink:href="https://picsum.photos/id/1000/800/800" x="0" y="0" width="100" height="100" />
          </pattern>
        </defs>
        <polygon points="0,0 0,100 100,100 50,0" fill="url(#img1)" class="hide">
            <animate attributeName="points" begin="start.click" end="stop.click"  dur="5s" values="0,0 0,100 100,100 50,0 ; 50,40 30,60 80,10 50,70; 0,0 0,100 100,100 50,0" keyTimes="0;0.5;1" repeatCount="indefinite"/>
        </polygon>
    </svg>
</body>

If you prefer a CSS-only solution, achieving this effect is quite straightforward with just a few lines of code:

img {
  width: 400px;
  display:block;
  clip-path: polygon(0 0, 0 100%, 100% 100%, 50% 0);
  animation:change 5s linear infinite paused;
}
input:checked + img {
  animation-play-state:running;
}

@keyframes change{
  50% {
    clip-path: polygon(50% 40%, 30% 60%, 80% 10%, 50% 70%)
  }
}
<input type="checkbox" >
<img src="https://picsum.photos/id/1000/800/800">

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to view the redirects initiated by the browser on different tabs?

Is there a way to trace the redirects initiated by the browser in separate tabs? One instance would be when a downloaded file triggers a new tab to open. ...

Title Bar: Excessive gap; inaccurate navigation to nearby link

I've been trying to align a horizontal navigation bar right below a banner and link directly to specific sections on the same page. No matter what I do, I can't seem to remove the white space between the banner image and the navigation bar. It ...

Utilizing Next.js Image Component to Apply CSS for Width and Height Settings

Currently, I am utilizing the Image component provided by Next.js, which requires setting specific width and height values. To address this, I am incorporating Tailwind CSS utility classes to establish the desired dimensions. <Image src={imageSr ...

Looking for a substitute for iframes when using jQuery UI tabs?

I currently have a Spring-based web application that integrates with jQuery Tabs. What I'm doing is building a data string with specific parameters and appending it to a URL: var hrefData = "?" + item1 + "&" + item2 + "&" + item3; var href = ...

Strategies for detecting when a child checkbox is clicked within a parent li element

Below is a snippet of Vue Code illustrating the structure we're working with: <template> <li class="mm-product-item" v-on:click="edit(item)"> <p class="mm-product-info input-field"> <input ...

Steps to remove a row from a table in a ReactJS component

I'm struggling with implementing a delete operation for table rows and encountering errors. Any assistance in resolving this issue would be greatly appreciated. Since I am unsure how to set an auto-incrementing id, I used Date.now(). Now my goal is t ...

"Implementing responsive design with Bootstrap's carousel through media queries

I'm having some trouble creating a basic carousel using bootstrap because the images are not displaying full screen. Do I need to use media queries to achieve this and if so, how can I implement them? <div id="myCarousel" class="carousel slide" da ...

Attempting to bind an input parameter using NgStyle in Angular version 2 and above

Issue: I am in need of a single component (spacer) with a width of 100% and a customizable height that can be specified wherever it is used in the HTML (specifically in home.html for testing): number 1 <spacer height="'200px'"></spa ...

Enhance the Bootstrap Sass by integrating a color scheme that seamlessly complements all elements such as backgrounds, text, and borders throughout the

Hi there, I'm having an issue with Bootstrap and Sass when trying to add new colors to my theme map. Can anyone help me figure out what I might be doing wrong? Thank you in advance. I've been attempting to include a new color in the theme-colo ...

Comparison of getComputedStyle() and cssText functionality between Internet Explorer and Firefox

Take a look at this example to see the issue in action. I'm attempting to access the cssText property of a <div> using window.getComputedStyle(element) (which provides a CSSStyleDeclaration object). While this works smoothly in Chrome, it' ...

Exploring the Differences Between Nth-CSS and Jquery

What are the advantages of using the nth function in CSS instead of applying it through jQuery, especially considering its compatibility with IE? Is it better to simply use jQuery from the start and avoid using it in a stylesheet altogether? Hopefully thi ...

:first-child that does not have the class .disabled

Does anyone have a solution for selecting the initial item in a list that does not possess the "disabled" class? ...

Using JavaScript to show content in a textarea field

In my index.jsp file, I have implemented the following code to populate two textareas, INPUT_TEXT and INPUT_TEXT2, with processed data. This involves passing the text through a servlet, a Java class, and finally displaying the preprocessed results in the s ...

Determining User Consent: A Straightforward Guide to Identifying Previously Accepted Cookie Consent

I've been tasked with adding a cookie to a website. The site only tracks the number of visits, so there is no user-specific data available for cookie assignment or acknowledgment tracking. Is it possible to show the cookie notification only to users ...

Problems with the navigation bar scrolling in Bootstrap

The project I'm currently working on is located at zarwanhashem.com If you'd like to see my previous question along with the code, you can check it out here: Bootstrap one page website theme formatting problems Although the selected answer help ...

What are effective methods for testing HTML5 websites?

I'm currently working on a project to create a dynamic website using HTML5. The first page will prompt the user for specific inputs, and the following page will adjust accordingly based on these inputs. My main concern now is how to effectively test ...

Managing browser back button functionality

I've been struggling to find a solution for handling the browser back button event. I would like to prompt the user with a "confirm box" if they click on the browser back button. If they choose 'ok', I need to allow the back button action, ...

Import JSON Data into Angular-nvD3 Chart (AngularJS)

I am seeking help to load encoded JSON Data retrieved from a database via queries into an Angular-nvD3 graph. I am unsure about the best approach to achieve this task. The encoded JSON data is fetched using API queries from a database table called PRODUCT ...

Incorporating HTML into the Ajax Response

I recently encountered a strange issue with a service that returns an HTML page as the AJAX response. This page contains a form that is automatically triggered by scripts within the page, resulting in a POST request being sent when the page is rendered. My ...

What is the process for configuring my form to automatically send to my email upon clicking the send button?

I found this code snippet on a website and I'm trying to figure out how to make the 'Send!' button redirect users to my email address with their message, name, and email included. Can anyone help me solve this issue? I attempted to add my e ...