gpt4 book ai didi

javascript - 转换不适用于使用 CSS 和 JS 提交的输入类型

转载 作者:行者123 更新时间:2023-11-29 19:03:09 25 4
gpt4 key购买 nike

我创建了两个按钮 Let's Go!click me。一个是 input type submit,第二个是 anchor taganchor 标记 的转换在用户单击它时有效,但 input type submit 在单击时不起作用。

检查下面的脚本。还有其他简单的方法可以解决这个问题吗?

const isMobile = window.navigator.userAgent.match(/Mobile/) && window.navigator.userAgent.match(/Mobile/)[0] === "Mobile";
const event = isMobile ? "touchstart" : "click";
const button = document.querySelectorAll('*[data-animation="ripple"]'),
container = document.querySelector(".container");
for (var i = 0; i < button.length; i++) {
const currentBtn = button[i];
currentBtn.addEventListener(event, function(e) {
e.preventDefault();
const button = e.target,
rect = button.getBoundingClientRect(),
originalBtn = this,
btnHeight = rect.height,
btnWidth = rect.width;
let posMouseX = 0,
posMouseY = 0;
if (isMobile) {
posMouseX = e.changedTouches[0].pageX - rect.left;
posMouseY = e.changedTouches[0].pageY - rect.top;
} else {
posMouseX = e.x - rect.left;
posMouseY = e.y - rect.top;
}
const baseCSS = `position: absolute;
width: ${btnWidth * 2}px;
height: ${btnWidth * 2}px;
transition: all linear 700ms;
transition-timing-function:cubic-bezier(0.250, 0.460, 0.450, 0.940);border-radius: 50%;
background: var(--color-ripple);
top:${posMouseY - btnWidth}px;
left:${posMouseX - btnWidth}px;
pointer-events: none;
transform:scale(0)`

var rippleEffect = document.createElement("span");
rippleEffect.style.cssText = baseCSS;

//prepare the dom
currentBtn.style.overflow = "hidden";
this.appendChild(rippleEffect);

//start animation
setTimeout(function() {
rippleEffect.style.cssText = baseCSS + `transform:scale(1); opacity: 0;`;
}, 5);

setTimeout(function() {
rippleEffect.remove();
//window.location.href = currentBtn.href;
}, 700);
})
}
:root {
/* if u want to change the color of
* the ripple change this value
*/
--color-ripple: rgba(255, 255, 255, 0.8);
}

.container {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
height: 50px;
width: 200px;
margin: auto;
}

*[data-animation="ripple"] {
position: relative;
/*Position relative is required*/
height: 100%;
width: 100%;
display: block;
outline: none;
padding: 20px;
color: #fff;
text-transform: uppercase;
background: linear-gradient(135deg, #e570e7 0%, #79f1fc 100%);
box-sizing: border-box;
text-align: center;
line-height: 14px;
border: none;
font-weight: 200;
letter-spacing: 1px;
text-decoration: none;
box-shadow: 0 5px 3px rgba(0, 0, 0, 0.3);
cursor: pointer;
/*border-radius: 50px;*/
-webkit-tap-highlight-color: transparent;
}

*[data-animation="ripple"]:focus {
outline: none;
}

*[data-animation="ripple"]::selection {
background: transparent;
pointer-events: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="container">
<a data-animation="ripple">Click Me</a>
</div>
<input type="submit" name="searchbtrn" value="Let’s Go!" data-animation="ripple" width=100>

最佳答案

如评论中所述,它不起作用的原因是因为您试图将一个元素附加到 input,这是无法完成的。

一个解决方案是将 input 替换为 button,但您也声明了您不想这样做。

我能想到的唯一其他解决方案是用 span 包装您的 input 并将波纹附加到此 span。效果完成后,解包input。我已在下面的示例中完成此操作。

const isMobile = window.navigator.userAgent.match(/Mobile/) && window.navigator.userAgent.match(/Mobile/)[0] === "Mobile";
const event = isMobile ? "touchstart" : "click";
const button = document.querySelectorAll('*[data-animation="ripple"]'),
container = document.querySelector(".container");
for (var i = 0; i < button.length; i++) {
const currentBtn = button[i];
currentBtn.addEventListener(event, function(e) {
e.preventDefault();
const button = e.target,
rect = button.getBoundingClientRect(),
originalBtn = this,
btnHeight = rect.height,
btnWidth = rect.width;
let posMouseX = 0,
posMouseY = 0;
if (isMobile) {
posMouseX = e.changedTouches[0].pageX - rect.left;
posMouseY = e.changedTouches[0].pageY - rect.top;
} else {
posMouseX = e.x - rect.left;
posMouseY = e.y - rect.top;
}
const baseCSS = `position: absolute;
width: ${btnWidth * 2}px;
height: ${btnWidth * 2}px;
transition: all linear 700ms;
transition-timing-function:cubic-bezier(0.250, 0.460, 0.450, 0.940);border-radius: 50%;
background: var(--color-ripple);
top:${posMouseY - btnWidth}px;
left:${posMouseX - btnWidth}px;
pointer-events: none;
transform:scale(0)`;

var rippleEffect = document.createElement("span");
rippleEffect.style.cssText = baseCSS;

//prepare the dom
currentBtn.style.overflow = "hidden";
var btn = this;
if (btn.tagName === "INPUT") {
$(btn).wrap('<span class="ripple-wrap"></span>').after(rippleEffect);
}
else {
btn.appendChild(rippleEffect);
}

//start animation
setTimeout(function() {
rippleEffect.style.cssText = baseCSS + `transform:scale(1); opacity: 0;`;
}, 5);

setTimeout(function() {
rippleEffect.remove();
if (btn.tagName === "INPUT") {
$(btn).unwrap();
}
//window.location.href = currentBtn.href;
}, 700);
})
}
:root {
/* if u want to change the color of
* the ripple change this value
*/
--color-ripple: rgba(255, 255, 255, 0.8);
}

.container {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
height: 50px;
width: 200px;
margin: auto;
}

*[data-animation="ripple"] {
position: relative;
/*Position relative is required*/
height: 100%;
width: 100%;
display: block;
outline: none;
padding: 20px;
color: #fff;
text-transform: uppercase;
background: linear-gradient(135deg, #e570e7 0%, #79f1fc 100%);
box-sizing: border-box;
text-align: center;
line-height: 14px;
border: none;
font-weight: 200;
letter-spacing: 1px;
text-decoration: none;
box-shadow: 0 5px 3px rgba(0, 0, 0, 0.3);
cursor: pointer;
/*border-radius: 50px;*/
-webkit-tap-highlight-color: transparent;
}

*[data-animation="ripple"]:focus {
outline: none;
}

*[data-animation="ripple"]::selection {
background: transparent;
pointer-events: none;
}

.ripple-wrap {
position: relative;
display: block;
overflow: hidden;
box-shadow: 0 5px 3px rgba(0, 0, 0, 0.3);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="container">
<a data-animation="ripple">Click Me</a>
</div>
<input type="submit" name="searchbtrn" value="Let’s Go!" data-animation="ripple" width=100>

关于javascript - 转换不适用于使用 CSS 和 JS 提交的输入类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45327176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com