HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div class="container">
<div class="dialog">
dialog content
<div class="drag-bar-left"></div>
<div class="drag-bar-right"></div>
<div class="drag-bar-top"></div>
<div class="drag-bar-bottom"></div>
<div class="mask-drag"></div>
</div>
</div>
*{
padding:0;
margin:0;
}
.container{
width:100%;
height:100%;
position:relative;
}
.dialog{
width:150px;
height:150px;
position:absolute;
left:50px;
top:50px;
background:gray;
}
.drag-bar-left{
background:transparent;
position: absolute;
left:0;
top:0;
bottom:0;
width:10px;
cursor: move;
display: block;
}
.drag-bar-right{
background:transparent;
position: absolute;
right:0;
top:0;
bottom:0;
width:10px;
cursor: move;
display: block;
}
.drag-bar-top{
background:transparent;
position: absolute;
left:0;
top:0;
right:0;
height:10px;
cursor: move;
display: block;
}
.drag-bar-bottom{
background:transparent;
position: absolute;
left:0;
right:0;
bottom:0;
height:10px;
cursor: move;
display: block;
}
/* 拖动时遮住整个弹框放置鼠标触发了里面的按钮 */
.mask-drag{
position: absolute;
cursor: move;
user-select: none;
left:0;
top:0;
}
// carried out
initDragDialog();
/*
* DOM selector
* @param selector {String} css selector for element
* @param context {Element} root element
* usage: $$('head')
* result: You will get <head> element
*/
function $$(selector, context) {
context = context || document
const elements = context.querySelectorAll(selector)
return elements.length == 1
? Array.prototype.slice.call(elements)[0]
: Array.prototype.slice.call(elements)
}
/**
* The pop-up window supports dragging
*/
function initDragDialog(){
// Drag and drop function (mainly to trigger three events: onpointerdown\onpointermove\onpointerup)
const drag = $$('.dialog');
const divMask = $$('.mask-drag');
const dragBarTop = $$('.drag-bar-top')
const dragBarBottom = $$('.drag-bar-bottom')
const dragBarLeft = $$('.drag-bar-left')
const dragBarRight = $$('.drag-bar-right')
dragBarTop.addEventListener('pointerdown',pointerDownLisenter)
dragBarBottom.addEventListener('pointerdown',pointerDownLisenter)
dragBarLeft.addEventListener('pointerdown',pointerDownLisenter)
dragBarRight.addEventListener('pointerdown',pointerDownLisenter)
// When you click on an object, you can use the drag object. Move and up are the global area, that is, the entire document is common. The document object should be used instead of the drag object (otherwise, when the drag object is used, the object can only move to the right or down )
function pointerDownLisenter (e) {
e = e || window.event // Compatible with IE browser
const diffX = e.clientX-drag.offsetLeft // The distance from the moment the mouse clicks on the object relative to the left border of the object = the distance of the position when clicked relative to the leftmost left of the browser - the distance of the left border of the object relative to the leftmost left of the browser
const diffY = e.clientY-drag.offsetTop
/* Low version IE bug: When an object is dragged out of the browser’s visual window, a scroll bar will appear,
The solution is to use two unique methods setCapture()\releaseCapture() in IE browser, these two methods,
You can let the mouse slide outside the browser to capture the event, and our bug is when the mouse moves out of the browser,
The function that the limit is exceeded becomes invalid. In this way, this problem can be solved. Note: These two methods are used in onpointerdown and onpointerup */
if (typeof drag.setCapture !=='undefined') {
drag.setCapture()
}
// Cover the pop-up window
divMask.style.width = '100%';
divMask.style.height = '100%';
// Prevent the selected event from being triggered when the dragging speed is too fast
document.body.style.userSelect ='none'
document.onpointermove = function (e) {
e = e || window.event // Compatible with IE browser
let left = e.clientX-diffX
let top = e.clientY-diffY
// Control the range of dragged objects can only be in the browser window, and scroll bars are not allowed
// if (left <0) {
// left = 0
// } else if (left> window.innerWidth-drag.offsetWidth) {
// left = window.innerWidth-drag.offsetWidth
// }
// if (top <0) {
// top = 0
// } else if (top> window.innerHeight-drag.offsetHeight) {
// top = window.innerHeight-drag.offsetHeight
// }
if (left < -drag.offsetWidth + 100) {
left = -drag.offsetWidth + 100
} else if (left > window.innerWidth - 100) {
left = window.innerWidth - 100
}
if (top < -drag.offsetHeight + 100) {
top = -drag.offsetHeight + 100
} else if (top > window.innerHeight - 100) {
top = window.innerHeight - 100
}
// Regain the distance of the object when moving, and solve the phenomenon of shaking when dragging
drag.style.left = left +'px'
drag.style.top = top +'px'
drag.style.transform =''
}
document.onpointerup = function (e) {// no longer move when the mouse pops up
this.onpointermove = null
this.onpointerup = null // Prevent the mouse from looping after it pops up (that is, prevent the mouse from moving when it is put on)
// Fix low version ie bug
if (typeof drag.releaseCapture !=='undefined') {
drag.releaseCapture()
}
// Restore the hidden mask layer
divMask.style.width = '0';
divMask.style.height = '0';
document.body.style.userSelect =''
}
}
}
Also see: Tab Triggers