Reputation: 26085
I often want to view the styles of an element that appears only when dragging or when the mouse is clicked (mousedown
event). How can I view the element's style using Google Chrome's developer tools?
Upvotes: 66
Views: 21432
Reputation: 625
You can run this code inside the console, then draging and wait for the callback function to run
(()=>{
setTimeout(()=>{
debugger
}, 5000)
})()
Upvotes: 1
Reputation: 31
In addition to @Davids answer, it might be worth mentioning, that you need to add a eventlistener somewhere in your code as well or simply put it in the console before
Example:
document.onclick=function(){};
Upvotes: 0
Reputation: 11012
From the DevTools Go to the lowest element that will wrap your draggable item
Right click this element and chose "Store as global variable" it'll be referred to from the console as temp1
Write in the console this command - let myInterval = setInterval(() => console.log(temp1.cloneNode(true)), 1000)
At this stage you can see the element details in the console whem you drag it.
When you don't need to inspect it any more run from the console - clearInterval(myInterval)
.
Instead of section 2 you can run the follow command and select your draggable element with the appropriate query selector - let myInterval = setInterval(() => console.log(document.querySelector(/* your query goes here */)?.cloneNode(true)), 1000)
Upvotes: 1
Reputation: 61
In case anyone encountered this question in the future, I have another solution for this. This solution is kinda same with the most upvoted answer, but it doesn't require any keydown, just simply drag:
After that, whenever you start to drag an element, the browser window will pause for debugging, then you can inspect the element's CSS styles freely.
Note: I tested this on Chrome version 80, but I think it works in older version though.
Edited:
Just now I figured out dragover breakpoints doesn't work in certain condition, e.g., if you want to inspect styles after the dragged item reached another element. For that situation, you may try different listeners as specify in Drag / drop, such as drop.
Upvotes: 6
Reputation: 7320
One way of doing it is to open the elements panel then right click while dragging. This opens the contextual menu and "pauses" the mouse move/hover effect. Then after right clicking, go back to the elements panel and search for the element using the find feature.
This can also be used to inspect hover effects (it's just faster than other methods)
This can be tested here for example https://jqueryui.com/draggable/#visual-feedback
Upvotes: 1
Reputation: 203
dragMethod() {
setTimeout( () => {
debugger;
}, 500)
}
This will suspend the drag action so you can proceed to inspect as normal.
Upvotes: 5
Reputation: 15984
You can simply press F8
while dragging (and developer tools is open)
Upvotes: 31
Reputation: 1759
Open the developer tools.
Go to "Sources":
Expand "Event Listener Breakpoints" on the right:
Add a listener for keydown events on the keyboard section:
Now start dragging the thing you want, and when it's time press any key on your keyboard and you'll be able to inspect the dragable element.
Upvotes: 153
Reputation: 1696
Put a breakpoint in the code - inside of the mousedown
event callback.
This will freeze the app when you begin dragging, and then you can tab over the the Element
section of the inspector to use it like you normally would, only now it's frozen at the beginning of the drag.
EDIT: You should put the breakpoint on a line below where the new elements you want to inspect are created, so the elements are on the DOM by the time you freeze.
// Raw event
someElement.addEventListener('mousedown', function(ev) {
// Put a breakpoint on any of the lines in here
}, false);
// jQuery for prudence
$(someSelector).on('mousedown', function(ev) {
// Put a breakpoint on any of the lines here
});
Upvotes: 0