Kar*_*cha 5 css jquery animation slidetoggle
我正在使用slideToggle();
方法,它有效,我只想从图像的中间点滑入和滑出,而不是向上滑到左角。我也不想要 ZoomIn 或 ZoomOut 事件。
$(document).ready(function() {
$('.btn').click(function() {
$('img').stop().slideToggle(5000);
});
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<button class="btn btn-default">My Button</button>
<br><br><br>
<img class="img-responsive" src="http://news.stanford.edu/news/2012/september/images/online_keyboard_news.jpg">
Run Code Online (Sandbox Code Playgroud)
小智 1
作为使用 jQuery 动画的替代方案,您所寻找的内容(假设我理解您所解释的内容)可以通过稍微更改 DOM 结构并添加几行 css 来完成。
const toggleButton = document.getElementById("toggle-button");
const imageContainer = document.getElementById("image-container");
toggleButton.addEventListener("click", () => {
imageContainer.classList.toggle("show");
});
Run Code Online (Sandbox Code Playgroud)
button {
position: relative;
margin: 20px;
}
/* Create a wrapper element that matches the image size */
/* and set it to the desired image reveal size */
.wrapper {
width: 600px;
height: 400px;
position: relative;
margin: 0 auto;
}
/* Add the image container element and set it to the center of the wrapper */
/* set the width and height to 0 so the image isn't visible */
.image {
top: 50%;
left: 50%;
width: 0;
height: 0;
position: absolute;
background: url(http://news.stanford.edu/news/2012/september/images/online_keyboard_news.jpg)
no-repeat center center;
transition-duration: 1000ms;
transition-timing-function: cubic-bezier(0.5, 0, 1, 1);
}
/* When we want to show the image animate the width and height */
/* to fit the container as well as adjust the margin to keep it centered */
.image.show {
width: 100%;
height: 100%;
position: absolute;
margin-left: -300px; /* set to half of the containing elements width */
margin-top: -200px; /* set half of the containing elements height */
transition-timing-function: cubic-bezier(0, 0, 0.5, 1);
}
Run Code Online (Sandbox Code Playgroud)
<button id="toggle-button">
Toggle Image
</button>
<div class="wrapper">
<div class="image show" id="image-container"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
我创建了一个快速示例,说明如何仅使用 javascript 与 css 一起使用来切换图像元素上的类。
CSS 中的注释描述了这是如何完成的。