I forgot how this is done.
Code: https://jsfiddle.net/3jr8otmg/1/
$(document).ready(function () {
$("#btn").on("click", function () {
var viewport = 640;
if ("" != $("#input").val()){
viewport = $("#input").val();
}
var str = $('#data').val();
var result = str.replace(/(\d*\.\d+|\d+)px/gi, function (px) {
var pointNum = parseFloat(px);
var x = convertPXToVW(pointNum, viewport);
return x + 'vw';
});
$('#result').val(result).select();
document.execCommand("copy");
$(".dasaochep").addClass("fadeIn"),
$(".dasaochep").removeClass("fadeOut"),
setTimeout(function () {
$(".dasaochep").removeClass("fadeIn"),
$(".dasaochep").addClass("fadeOut");
}, 500);
});
});
function convertPXToVW(px, width) {
var a = px * (100 / width);
return Math.round(a * 10000) / 10000;
}
Do you mean how do you convert jQuery to vanilla JS?
Detailed instructions on converting jQuery to JavaScript is found at:
Frequently I see on here people asking to deal with jQuery, for example with converting it to vanilla JavaScript.
As a result I thought that I’d provide a worked example of converting code from jQuery to vanilla JavaScript, using jQuery that is a bit more complex than the standard standard conversion tutorials. Along with event handlers this guide uses custom backgrounds that change to match the different tab colors, and deals with animation too.
The original code is from a tabbed panel on cod…
2 Likes
This:
$(document).ready(function() {
// code
})
Becomes: this
document.addEventListener('DOMContentLoaded', function() {
// code
})
Yes that’s right. But, you have no need for DOMContentLoaded. Scripting code is run at the end of the <body>
, which happens before the DOMContentLoaded event.
Using the DOMContentLoaded event delays the running of the code.
Removing that event results in even faster running code with no complications.
// document.addEventListener('DOMContentLoaded', function() {
code
// })
1 Like
system
Closed
September 29, 2021, 1:42pm
6
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.