You cannot use the solution proposed in the page you linked because SVG does not support element.offsetTop.
An easy alternative is using the property
top of Element.getBoundingClientRect():
const svg = d3.select("svg"); const offset = svg.node().getBoundingClientRect().top; console.log(offset)
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script> <img width="600" height="600"/> <svg width="1500" height="650"/>
If you still want to use
offsetTop, wrap the SVG in an HTML container, like a
const svg = d3.select("svg"); const offset = svg.node().parentNode.offsetTop; console.log(offset)
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script> <img width="600" height="600" /> <div> <svg width="1500" height="650" /> </div>
- How to get the svg position within document?
- How do I get the d3 svg points to stay in their correct position on the leaflet map when zooming?
- How do I get the width of an svg element using d3js?
- In d3, how to get the interpolated line data from a SVG line?
- How can I get the natural width and height of an svg image element?
- How do I get D3 Data Driven Document to display a moment.js datetime in the correct time zone?
- How to get web element (id) from the element position in D3.js force graph
- What is the fastest way to get SVG element given a position (x,y)?
- How to get SVG path data of TopoJSON feature without adding it to the DOM?
- How to get the width and height of an image? ( getBBox() size of SVG return 0 )
- NVD3.js Get Data Point's Screen Position Within SVG Chart
- How to get position of empty SVG group?
- How can I get the dom svg element from d3 node data?
- get the position details of a div inside an svg
- How to get a circle position in a g element in SVG using D3.js
- How to get the x position of a bar on d3 paired bar graph for tooltip update
- How to get element on mouse position within d3.xhr and async queue?
- How to get SVG child element position relative to SVG position on page, D3.js?
- How to get the position and width of a tspan element
- How can i get the new X and Y position after rotating?
- How to get the zoom level of an svg image using d3
- How to get the id of svg that was draw over other svg and delete it
- How to setup svg to change position when resizing the window?
- How do I change the position of a circle svg element using the transform attribute?
- How can i get the id of a polygon in an SVG file?
- How to return the numerical position of a piece of data within a dataset?
- D3.js: How to get the computed width and height for an arbitrary element?
- How to access the DOM element that correlates to a D3 SVG object?
- How to get coordinates of an svg element?
- d3.js - How can I set the cursor to hand when mouseover these elements on SVG container?
More Query from same tag
- d3 Uncaught TypeError: n.domain is not a function
- Smooth Tween Font Size
- d3.js concentric ring chart
- Use variable to call data column in dataset d3.js
- d3.js ticks function giving more elements than needed
- Implementing d3 Codeflower source code
- d3.js -- Axis label help (from v3 to v4)
- How to implement collision avoidance of points in d3.js v5?
- D3 (v3.4) edge bundle chart and update button to load a new dataset
- How can I handle data filtering on a button click on D3.js?
- D3 js V4 - Scatterplot configuration
- change lines into scatter plot in d3.js
- Data with custom date dimple.js
- convert local variable to global in d3.js
- Running jsdom in Nashorn
- How to insert new data into a bar chart from input text field?
- Center text over curved links in a force-directed graph
- How to use json data instead of tsv file in d3 multi line charts?
- Why does the mouseover method don't work on node's text in a dialog?
- D3.js collapsable tree - visualise set number of levels
- d3 barchart animates on load, but not on update
- How to animate the radius and the arc length of a pie chart in d3 (at the same time)?
- Axis-range-slider alignment
- How to handle data occlusion in d3js?
- Make an external Script available to all members of qooxdoo Object
- D3 circle pack: html in text attribute
- d3.json in d3-ng2-service?
- Create scatter plot with ordinal scale in d3 version 4
- D3.js conflict with Boostrap